Spring Interview Questions and Answers
Question - 101 : - How to get ServletConfig and ServletContext objects in spring bean?
Answer - 101 : -
This can be done by either implementing the spring-aware interfaces or by using the @Autowired annotation.
- @Autowired
- private ServletContext servletContext;
- @Autowired
- private ServletConfig servletConfig;
Question - 102 : - How is it possible to use the Tomcat JNDI DataSource in the Spring applications?
Answer - 102 : -
To use the servlet container which is configured in the JNDI (Java Naming and Directory Interface) DataSource, the DataSource bean has to be configured in the spring bean config file and then injected into the beans as dependencies. Post this, the DataSource bean can be used for performing database operations by means of the JdbcTemplate. The syntax for registering a MySQL DataSource bean:
Question - 103 : - What will be the selection state of a checkbox input if the user first checks the checkbox and gets validation errors in other fields and then unchecks the checkbox after getting the errors?
Answer - 103 : -
The validation is generally performed during HTTP POST requests. During HTTP requests, if the state of the checkbox is unchecked, then HTTP includes the request parameter for the checkbox thereby not picking up the updated selection. This can be fixed by making use of a hidden form field that starts with _ in the Spring MVC.
Question - 104 : - What are the differences between @RequestParam and @PathVariable annotations?
Answer - 104 : -
- Even though both these annotations are used to extract some data from URL, there is a key difference between them.
- The @RequestParam is used to extract query parameters that is anything after “?” in the URL.
- The @PathVariable is used to extract the data present as part of the URI itself.]
- For example, if the given URL is http://localhost:8080/InterviewBit/Spring/SpringMVC/?format=json, then you can access the query parameter “format” using the @RequestParam annotation and /Spring/{type} using the @PathVariable, which will give you SpringMVC.
@RequestMapping("/Spring/{type}")
public void getQuestions(@PathVariable("type") String type,
@RequestParam(value = "format", required = false) String format){
/* Some code */
}
Question - 105 : - What is the Model in Spring MVC?
Answer - 105 : -
- Model is a reference to have the data for rendering.
- It is always created and passed to the view in Spring MVC. If a mapped controller method has Model as a parameter, then that model instance is automatically injected to that method.
- Any attributes set on the injected model would be preserved and passed to the View.
Question - 106 : - What is the use of @Autowired annotation?
Answer - 106 : -
@Autowired annotation is meant for the injection of a bean by means of its type along with methods and fields. This helps the Spring framework to resolve dependencies by injecting and collaborating the beans into another bean. For example, consider the below code snippet:
import org.Springframework.beans.factory.annotation.Autowired;
import java.util.*;
public class InterviewBit {
// Autowiring/Injecting FormatterUtil as dependency to InterviewBit class
@Autowired
private FormatterUtil formatterUtil;
public Date something( String value ){
Date dateFormatted = formatterUtil.formatDate(value);
return dateFormatted
}
}
/**
* Util class to format any string value to valid date format
*/
public class FormatterUtil {
public Date formatDate(String value){
//code to format date
}
}
Question - 107 : - What is the role of @ModelAttribute annotation?
Answer - 107 : -
The annotation plays a very important role in binding method parameters to the respective attribute that corresponds to a model. Then it reflects the same on the presentation page. The role of the annotation also depends on what the developer is using that for. In case, it is used at the method level, then that method is responsible for adding attributes to it. When used at a parameter level, it represents that the parameter value is meant to be retrieved from the model layer.
Question - 108 : - What is the importance of the web.xml in Spring MVC?
Answer - 108 : -
web.xml is also known as the Deployment Descriptor which has definitions of the servlets and their mappings, filters, and lifecycle listeners. It is also used for configuring the ContextLoaderListener. Whenever the application is deployed, a ContextLoaderListener instance is created by Servlet container which leads to a load of WebApplicationContext.