Spring Framework Interview Questions And Answers

Here I am listing frequently asked questions on Java Spring Framework. Its based on my personal experience and which I mostly asked while taking interview.

1) What is Spring Framework?

Ans - Spring is one of the most widely used Java EE framework. Spring framework core concepts are “Dependency Injection” and “Aspect Oriented Programming”.

2) What are benefits of Spring Framework?
Ans -

  • Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.
  • Inversion of control (IOC): Loose coupling is achieved in Spring, with the Inversion of Control technique.
  • Aspect oriented (AOP): It separates application business logic from system services. Enterprise applications have some common cross-cutting concerns.
  • MVC Framework: Model View Controller mechanism.

3) Which are the Spring framework modules?
Ans -
  • Core module
  • Context module
  • Bean module
  • Web module
  • JDBC module
  • ORM module
4) What is default scope of bean in Spring Framework ?

Ans - The default scope of bean in Spring Framework is Singleton.

5) Are Singleton beans in Spring framework thread safe ?

Ans - No. Singleton beans in Spring Framework are NOT thread safe.

6) Which DI would you suggest Constructor-based or setter-based DI?

Ans - You can use both Constructor-based and Setter-based Dependency Injection. The best solution is using constructor arguments for mandatory dependencies and setters for optional dependencies.

7) What are the bean scopes supported by Spring Framework ?

Ans - 
  • Singleton scope, Spring scopes the bean definition to a single instance per Spring IoC container.
  • Prototype scope, a single bean definition has any number of object instances.
  • Request scope, a bean is defined to an HTTP request. This scope is valid only in a web-aware Spring ApplicationContext.
  • Session scope, a bean definition is scoped to an HTTP session. This scope is also valid only in a web-aware Spring ApplicationContext.
  • Global-session scope, a bean definition is scoped to a global HTTP session. This is also a case used in a web-aware Spring ApplicationContext.
8) What is @Required annotation ?

Ans - This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws BeanInitializationException if the affected bean property has not been populated.

9) What is @Qualifier annotation ?

Ans - When there are more than one beans of the same type and only one is needed to be wired with a property, the @Qualifier annotation is used along with @Autowired annotation to remove the confusion by specifying which exact bean will be wired.

10) Types of the transaction management Spring support

Ans - 
  • Programmatic transaction management: This means that you have managed the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
  • Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.
11) Which Transaction management type is more preferable ?

Ans - Most users of the Spring Framework choose declarative transaction management.

12) How to handle exceptions in Spring MVC Framework ?

Ans - 
  • Controller Based – We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation.
  • Global Exception Handler – Exception Handling is a cross-cutting concern and Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler.
13) Where do you need @EnableWebMVC ?

Ans - The @EnableWebMvc annotation is required to enable Spring MVC when Java configuration is used to configure Spring MVC instead of XML. It is equivalent to <mvc: annotation-driven> in XML configuration.

14) When do you need @ResponseStatus annotation in Spring MVC ?

Ans - 
The @ResponseStatus annotation is required during error handling in Spring MVC and REST. Normally when an error or exception is thrown at server side, web server return a blanket HTTP status code 500 - Internal server error.

This may work for a human user but not for REST clients. You need to send them proper status code e.g. 404 if the resource is not found. That's where you can use @ResponseStatus annotation, which allows you to send custom HTTP status code along with proper error message in case of Exception.

In order to use it, you can create custom exceptions and annotated them using @ResponseStatus annotation and proper HTTP status code and reason.

For Example -

15) What is an HttpMessageConverter ?

Ans - HttpMessageConverter is a strategy interface that specifies a converter that can convert from and to HTTP requests and responses. Internally Spring MVC uses it to convert the Http request to an object representation and back.

16) How message converting is working in spring ?

Ans - “Content-Type” as application/json and HTTP request body as JSON data, the @RequestBody determines the appropriate converter and deserializes the incoming JSON request data into its corresponding Java entity as specified in the controller argument.

17) What is an Actuator ?

Ans - It brings production ready features for application like Monitoring our app, gathering metrics, understanding traffic or the state of our database.
We need to add spring-boot-starter-actuator dependency for this.

18) What is use of @Primary annotation ?

Ans - We use @Primary to give higher preference to a bean when there are multiple beans of the same type.

@Primary is also good if e.g. 95% of @Autowired wants a particular bean. That way, only the 

@Autowired that wants the other bean(s) need to specify @Qualifier. That way, you have primary beans that all autowired wants, and @Qualifier is only used to request an "alternate" bean.

19) What is Spring HATEOAS ?

Ans - The Spring HATEOAS project is a library of APIs that we can use to easily create REST representations that follow the principle of HATEOAS (Hypertext as the Engine of Application State).

Generally speaking, the principle implies that the API should guide the client through the through the application by returning relevant information about the next potential steps, along with each response.

20) When do you need @ResponseBody annotation in Spring MVC ?

Ans - The @ResponseBody annotation can be put on a method to indicates that the return type should be written directly to the HTTP response body (and not placed in a Model, or interpreted as a view name).

21) What is Difference between BeanFactory and ApplicationContext ?

Ans - 
  • BeanFactory provides basic IOC and DI features while ApplicationContext provides advanced features.
  • One of the popular implementation of BeanFactory interface is XMLBeanFactory while one of the popular implementation of ApplicationContext interface is ClassPathXmlApplicationContext.
  • A bean factory lazily loads all beans, deferring bean creation until the getBean() method is called. An application context preloads all singleton beans upon context startup. By preloading singleton beans, you ensure that they will be ready to use when needed—your application won’t have to wait for them to be created.
  • Application contexts can publish events to beans that are registered as listeners while BeanFactory can’t.
For more Click Here

To grab course on Spring Framework Click Here