• +91 9723535972
  • info@interviewmaterial.com

Spring Interview Questions and Answers

Spring Interview Questions and Answers

Question - 91 : - What is the @Controller annotation used for?

Answer - 91 : -

The @Controller is a stereotype Spring MVC annotation to define a Controller.

Question - 92 : - What is ContextLoaderListener and what does it do?

Answer - 92 : -

  • The ContextLoaderListener loads and creates the ApplicationContext, so a developer need not write explicit code to do create it. In short, it is a listener that aids to bootstrap Spring MVC.
  • The application context is where Spring bean resides. For a web application, there is a subclass called WebAppliationContext.
  • The lifecycle of the ApplicationContext is tied to the lifecycle of the ServletContext by using ContextLoaderListener. The ServletContext from the WebApplicationContext can be obtained using the getServletContext() method.

Question - 93 : - How can you achieve thread-safety in beans?

Answer - 93 : -

The thread safety can be achieved by changing the scope of the bean to request, session or prototype but at the cost of performance. This is purely based on the project requirements.

Question - 94 : - What is the significance of @Repository annotation?

Answer - 94 : -

@Repository annotation indicates that a component is used as the repository that acts as a means to store, search or retrieve data. These can be added to the DAO classes.

Question - 95 : - How is the dispatcher servlet instantiated?

Answer - 95 : -

The dispatcher servlet is instantiated by means of servlet containers such as Tomcat. The Dispatcher Servlet should be defined in web.xml The DispatcherServlet is instantiated by Servlet containers like Tomcat. The Dispatcher Servlet can be defined in web.xml as shown below:

 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 
 
   appServlet
   org.springframework.web.servlet.DispatcherServlet
   
     contextConfigLocation
     /WEB-INF/spring/appServlet/servlet-context.xml
   
   1
 

 
   InterviewBitServlet
   /
 

Here, the load-on-startup tag is 1 which indicates that the DispatcherServlet is instantiated whenever the Spring MVC application to the servlet container. During this process, it looks for the servlet-name-context.xml file and initializes beans that are defined in the file.

Question - 96 : - How is the root application context in Spring MVC loaded?

Answer - 96 : -

The root application context is loaded using the ContextLoaderListener that belongs to the entire application. Spring MVC allows instantiating multiple DispatcherServlet and each of them have multiple contexts specific to them. They can have the same root context too.

Question - 97 : - Where does the access to the model from the view come from?

Answer - 97 : -

The view requires access to the model to render the output as the model contains the required data meant for rendering. The model is associated with the controller that processes the client requests and finally encapsulates the response into the Model object.

Question - 98 : - Why do we need BindingResults?

Answer - 98 : -

BindingResults is an important Spring interface that is within the org.Springframework.validation package. This interface has a very simple and easy process of invocation and plays a vital role in detecting errors in the submitted forms. However, care has to be taken by the developer to use the BindingResult parameter just after the object that needs validation. For example:

@PostMapping("/interviewbit")
public String registerCourse(@Valid RegisterUser registerUser,
 BindingResult bindingResult, Model model) {
   if (bindingResult.hasErrors()) {
       return "home";
   }
   model.addAttribute("message", "Valid inputs");
   return "home";
}
The Spring will understand to find the corresponding validators by checking the @Valid annotation on the parameter.

Question - 99 : - What are Spring Interceptors?

Answer - 99 : -

Spring Interceptors are used to pre-handle and post-handle the web requests in Spring MVC which are handled by Spring Controllers. This can be achieved by the HandlerInterceptor interface. These handlers are used for manipulating the model attributes that are passed to the controllers or the views.
The Spring handler interceptor can be registered for specific URL mappings so that it can intercept only those requests. The custom handler interceptor must implement the HandlerInterceptor interface that has 3 callback methods that can be implemented:

  • preHandle()
  • postHandle()
  • afterCompletion()
The only problem with this interface is that all the methods of this interface need to be implemented irrespective of its requirements. This can be avoided if our handler class extends the HandlerInterceptorAdapter class that internally implements the HandlerInterceptor interface and provides default blank implementations.

Question - 100 : - Is there any need to keepspring-mvc.jar on the classpath or is it already present as part of spring-core?

Answer - 100 : -

The spring-mv.jar does not belong to the spring-core. This means that the jar has to be included in the project’s classpath if we have to use the Spring MVC framework in our project. For Java applications, the spring-mvc.jar is placed inside /WEB-INF/lib folder.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners