Spring Interview Q&A

Q. Which Spring annotation is used to map web request to Spring Controller method?

Ans. @RequestMapping

Q. What will happen if we don’t specify the method parameter in @RequestMapping

Ans. If we don’t specify the HTTP method parameter, then @RequestMapping will work for any HTTP request irrespective of HTTP method.

Q. What is the difference between Spring @Controller and @RestController annotations?

Ans. Although both the annotations are used to mark a class as controller however following are the differences.

  1. We don’t need to use @ResponseBody with each handler method once we annotate the class with @RestController.
  2. @Controller creates a Map of Model object and find view while @RestController simply returns object and object data written into HTTP Response as JSON or XML.

Q. Explain BeanFactory and ApplicationContext in Spring framework.

Ans. The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism for managing any type of object. ApplicationContext is a sub-interface of BeanFactory. It facilitate the following:

  1. Easier integration with Spring’s AOP feature.
  2. Message resource handling
  3. Event publication
  4. Application layer specific contexts such as the WebApplicationContext for use in WebApplicationContext.

To be precise, BeanFactory provides configuration framework and basic functionality whereas ApplicationContext adds more enterprise-specific functionality.

Q. Explain Spring IoC container.

Ans. The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in Spring XML configuration file, Java annotations, or Java code.

ClassPathXmlApplicationContext and FileSystemXmlApplicationContext are the implementations of ApplicationContext interface.

Q. What are various bean scope in spring?

Ans. Out of the box, the Spring Framework supports following five scopes, three of which are available only if we use a web-aware ApplicationContext.

Bean ScopesDescription
Singleton(Default Scope) The scope of the Spring singleton is best described as per container and per bean. This means that if we define one bean for a particular class in a single Spring container, then the Spring container creates exactly one  instance of the class defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.
PrototypeA new bean instance  is created every time a request for that specific bean is made. General rule is: use the prototype scope for all stateful beans and the singleton scope for stateless beans.
RequestScopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
SessionScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
Global SessionScopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

Note: For more detailed information please click here.

Q. When a prototype-scoped bean B is injected into a singleton-scoped bean A, and a method of bean A uses bean B, then how many instances of bean B would be created if the method is called twice.?

Ans. The container creates the singleton bean A only once and thus gets only one opportunity to set properties. Hence, the container cannot provide bean A with a new instance of bean B every time. That means only one instance of prototype-scoped bean B would be provided to singleton-scoped bean A.

To override this constraint, Lookup method injection can be used.

Q. What is difference between @Autowired, @Inject and @Resource?

Ans. All these annotations provide classes a declarative way to resolve dependency. Each of these annotations can resolve dependency either by field injection or by setter injection.

@Autowired@Inject@Resource
This annotation belongs to org.springframework.beans.factory.annotation packageBelongs to javax.inject.InjectBelongs to javax.annotation.Resource
@Autowired is part of the spring framework. This annotation has the following execution path listed by precedence.
1. Match by Type
2. Match by Qualifier
3. Match by Name
@Inject annotation belongs to JSR-330 annotations collection. This annotation has the following execution path listed by precedence.
1. Match by Type
2. Match by Qualifier
3. Match by Name
@Resource annotation is part of JSR-250 annotation collection and is packaged with Jakarta EE. This annotation has the following execution paths listed by precedence.
1. Match by name
2. Match by Type
3. Match by Qualifier