May 29, 2019

Srikaanth

Softtek Spring MVC Interview Questions Answers

How do Spring resolves view returned by ModelAndView class ?

Some Spring MVC questions are tricky e.g. Struts and Spring integration and can be only answered by experienced Java program with 2 to 4-year experience in Spring MVC framework.

What are different implementations of View interface you have used in Spring MVC?

ULBased View e.g. JSP , JSTLView,

How to escape HTML special characters using Spring MVC?

There are some methods in Spring tag library, can't remember now.

What does the ViewResolver class?

ViewResolver is an interface to be implemented by objects that can resolve views by name. There are plenty of ways using which you can resolve view names. These ways are supported by various in-built implementations of this interface. Most commonly used implementation is InternalResourceViewResolver class. It defines prefix and suffix properties to resolve the view component.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>
So with above view resolver configuration, if controller method return “login” string, then the “/WEB-INF/views/login.jsp” file will be searched and rendered.
Softtek Most Frequently Asked Spring MVC Latest Interview Questions Answers
Softtek Most Frequently Asked Spring MVC Latest Interview Questions Answers

What is a MultipartResolver and when its used?


Spring comes with MultipartResolver to handle file upload in web application. There are two concrete implementations included in Spring:

CommonsMultipartResolver for Jakarta Commons FileUpload
StandardServletMultipartResolver for Servlet 3.0 Part API
To define an implementation, create a bean with the id “multipartResolver” in a DispatcherServlet’s application context. Such a resolver gets applied to all requests handled by that DispatcherServlet.

If a DispatcherServlet detects a multipart request, it will resolve it via the configured MultipartResolver and pass on a wrapped HttpServletRequest. Controllers can then cast their given request to the MultipartHttpServletRequest interface, which permits access to any MultipartFiles.

How does Spring MVC provide validation support?

Spring supports validations primarily into two ways.

Using JSR-303 Annotations and any reference implementation e.g. Hibernate Validator
Using custom implementation of org.springframework.validation.Validator interface.

What is Spring MVC Interceptor and how to use it?

As you know about servlet filters that they can pre-handle and post-handle every web request they serve — before and after it’s handled by that servlet. In the similar way, you can use HandlerInterceptor interface in your spring mvc application to pre-handle and post-handle web requests that are handled by Spring MVC controllers. These handlers are mostly used to manipulate the model attributes returned/submitted they are passed to the views/controllers.

A handler interceptor can be registered for particular URL mappings, so it only intercepts requests mapped to certain URLs. Each handler interceptor must implement the HandlerInterceptor interface, which contains three callback methods for you to implement: preHandle(), postHandle() and afterCompletion().

Problem with HandlerInterceptor interface is that your new class will have to implement all three methods irrespective of whether it is needed or not. To avoid overriding, you can use HandlerInterceptorAdapter class. This class implements HandlerInterceptor and provide default blank implementations.

What is Spring Security?

Spring security framework focuses on providing both authentication and authorization in java applications. It also takes care of most of the common security vulnerabilities such as CSRF attack.

It’s very beneficial and easy to use Spring security in web applications, through the use of annotations such as @EnableWebSecurity. You should go through following posts to learn how to use Spring Security framework.

Name some of the design patterns used in Spring Framework?

Spring Framework is using a lot of design patterns, some of the common ones are:
  1. Singleton Pattern: Creating beans with default scope.
  2. Factory Pattern: Bean Factory classes
  3. Prototype Pattern: Bean scopes
  4. Adapter Pattern: Spring Web and Spring MVC
  5. Proxy Pattern: Spring Aspect Oriented Programming support
  6. Template Method Pattern: JdbcTemplate, HibernateTemplate etc
  7. Front Controller: Spring MVC DispatcherServlet
  8. Data Access Object: Spring DAO support
  9. Dependency Injection and Aspect Oriented Programming.
Can we send an Object as the response of Controller handler method?

Yes we can, using @ResponseBody annotation. This is how we send JSON or XML based response in restful web services.

How to upload file in Spring MVC Application?

Spring provides built-in support for uploading files through MultipartResolver interface implementations. It’s very easy to use and requires only configuration changes to get it working. Obviously we would need to write controller handler method to handle the incoming file and process it.

How to validate form data in Spring Web MVC Framework?

Spring supports JSR-303 annotation based validations as well as provide Validator interface that we can implement to create our own custom validator. For using JSR-303 based validation, we need to annotate bean variables with the required validations.

For custom validator implementation, we need to configure it in the controller class.

What is Spring MVC Interceptor and how to use it?

Spring MVC Interceptors are like Servlet Filters and allow us to intercept client request and process it. We can intercept client request at three places – preHandle, postHandle and afterCompletion.

We can create spring interceptor by implementing HandlerInterceptor interface or by extending abstract class HandlerInterceptorAdapter.

We need to configure interceptors in the spring bean configuration file. We can define an interceptor to intercept all the client requests or we can configure it for specific URI mapping too.

What is Spring JdbcTemplate class and how to use it?

Spring Framework provides excellent integration with JDBC API and provides JdbcTemplate utility class that we can use to avoid bolier-plate code from our database operations logic such as Opening/Closing Connection, ResultSet, PreparedStatement etc.

What is Aspect Oriented Programming (AOP)?

- Basically Aspect oriented programming complements object oriented programming by providing another way of programming model structure.
- In addition to classes, AOP gives you aspect, which enables modularization of concerns such as Transaction management or logging and can be separated out from the application business logic of the code (these kinds of concerns are termed as crosscutting concerns). AOP supports separating application business logic from System services.

What is IOC or Dependency Injection?

The basic concept of IOC (Dependency of Injection) is that you do not create your objects but describe how they should be created.

You don’t directly connect your component and services together in code but describe which services are needed by which component in configuration file.

You just need to describe the dependency, the Spring container is then responsible for hooking it all up.

When to use Dependency Injections?

There are different scenarios where you Dependency Injections are useful.

- You need to inject configuration data into one or more component.
- You need to inject the same dependency into multiple components.
- You need to inject different implementation of the same dependency.
- You need to inject the same implementation in different configuration.
- You need some of the services provided by container.

When you should not use Dependency Injection?

There were scenarios where you don’t need dependency injections e.g.
- You will never need a different implementation.
- You will never need different configurations.

What is Bean Factory in Spring?

- A Bean Factory is like a factory class that contains collections of beans. The Bean Factory holds bean definition of multiple beans within itself and then instantiates the bean when asked by client.
- Bean Factory is actual representation of the Spring IOC container that is responsible for containing and managing the configured beans.

Different Spring Bean Scope.

1. singleton : Return a single bean instance per Spring IOC container.
2. prototype : Return a new bean instance each time when requested.
3. request : Return a single bean instance per HTTP request.
4. session : Return a single bean instance per HTTP session.
5. global session : Return a single bean instance per global HTTP session and only valid when used in portlet context.

Subscribe to get more Posts :