Spring Interview Questions

Spring Interview Questions

This post is the part of our Interview Questions series and in this post, we will discuss some of the commonly asked Spring Interview Questions during a job interview.


1. Spring Beans And Dependency Injection

Spring Beans are the core of Spring Framework. Some questions related to Spring Beans are:


Q1. What is Spring Framework?

Spring is known as one of the most popular frameworks for Java Enterprise applications. Spring framework Dependency injection feature can be used on any Java based application (web or non-web).

The core of Spring framework is based on dependency injection design pattern which allows us to easily build decoupled systems.



Q2. What are the benefits of Spring?

Spring Framework makes it easier to build enterprise Java applications, below are some of the advantages of using it

  • IoC Container: Spring manage the lifecycle of the Spring Beans and configurations using it’s Ioc container, this makes project management and maintenance easier.
  • Versatile: Spring provides flexibility to integrate it with any Java based technologies.
  • Inversion of Control: Spring will take care of injecting required dependencies for our application, we are no longer required to manually search and inject required dependencies for our application.
  • Lightweight: Spring is divided into different modules with a minimal dependency on each other which make it really lightweight and have the flexibility to only use required modules for your application.
  • End to End Development: Spring Framework provides modules for all aspects of application development ranging from Dependency Injection, Persistence, Caching, Business, Web, REST, it’s an ecosystem for developing all type of Java based Enterprise applications.
  • Non-invasive: Spring does not force us to use any Spring based interface or to extend any Spring based class to use its features which give our code flexibility to be used in any other technology. e.g. We can inject dependency using @Inject annotation which is not Spring based annotation, there will be no change in case we want to use Guice as our dependency injection API.
  • Exception Handling: Spring Framework provides multiple options to handle exceptions in a better and more flexible way.



Q3. What is Dependency Injection?

Dependency Injection is a technique with which we can make our program loosely coupled and inject required dependencies by defining them as Beans using several annotations like @Service, @Component, @Repository, @Controller or a @Bean annotation.



Q4. What are types of Dependency Injection techniques supported in Spring?

Spring supports three types of Dependency Injection

  • Setter based Dependency Injection.
  • Constructor based Dependency Injection.
  • Field Injection


Q5. What does bean scopes Spring supports?

There are five bean scopes supported by Spring:

  1. Singleton
  2. Prototype
  3. Request
  4. Session
  5. Global-session


Q6. Explain bean scopes in Spring?

Bean scopes define the lifecycle of a Bean. More about bean scopes can be read

  1. singleton(default*)
    Scopes a single bean definition for a single object instance per Spring IoC container.
  2. prototype
    Scopes a single bean definition for any number of object instances.
  3. request
    Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have 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.
  4. session
    Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
  5. global session
    Scopes 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.

For more information read Bean scopes


Q7. What is the recommended way to inject beans in Spring?

The recommended way to inject dependencies into Spring components is by using constructor injection. This should be followed by any compulsory dependencies needed. For optional dependencies, setter injection should be used.


Q8. What are the different modules/subprojects of the Spring?

  1. Spring Core.
  2. Spring JDBC
  3. Spring ORM
  4. Spring AOP
  5. Spring MVC


Q9. What Design patterns are used in Spring framework?

  1. Singleton pattern in Singleton scoped beans
  2. Model View Controller in Spring MVC
  3. Data Access Object in Spring DAO support
  4. Front Controller in Spring Dispatcher Servlet
  5. Template Method Pattern in Spring JDBC support
  6. Adapter Pattern in Spring MVC and Spring Web
  7. Proxy Pattern in Spring AOP support
  8. Factory Pattern in Bean Factory classes


Q10. What are Spring profiles?

Spring profiles allow us to configure Spring Beans based on the profile of dev, QA, staging, production etc. The use of profiles is that we can make same Spring app which points to different databases or message broker instances with a difference of just a flag. Please read our article on Spring Profiles for more detail.


Q11. What does @Required annotation do?

@Required annotation marks a bean as a required bean which means it needs to be configured at configuration time. Otherwise, the Spring container will throw a BeanInitializationException exception.



Q12. What does @Autowired annotation do?

The @Autowired annotation is used with fields or methods for injecting a bean by type. This annotation allows Spring to resolve and inject collaborating beans into the required bean.



Q13. In how many ways we can handle Exceptions in a Spring project?

  1. By using @ExceptionHandler annotation at the Controller level. This helps in managing an exception in a particular controller without affecting complete app flow.
  2. Using HandlerExceptionResolver, we can handle exceptions at the complete application level.
  3. By using @ControllerAdvice annotation, we can configure Exception handlers for application-level control and still keep the code very clean and modular.


Q14. What is the difference between annotations like @Component, @Controller, @Service and @Repository?

Out of all, @Component annotation is a generic annotation. So anything which is a Controller, Service or a Repository, it’s a Component as well. For specifics, let’s look at each of one of these:

  • @Controller: This annotation enables the detection of @RequestMapping annotation in the class. It also indicates that this class is a Controller.
  • @Service: It indicates that this class handles the business logic of the application and ought to call the @Repository classes for DB interaction.
  • @Repository: This indicates that the class defines a DB interface. It makes sure that the platform-specific exceptions are caught again and thrown again as Spring specific unchecked exceptions



In this article, we discussed some the commonly asked Spring Interview Questions during a job interview.We will be covering Interview questions of the other modules of the Spring framework in a different post.

Let us know in case you feel any modification or edit is required or you think that we are missing some important questions.

Leave a Reply

1 Comment on "Spring Interview Questions"

Notify of
Sort by:   newest | oldest | most voted

[…] Spring Interview Questions […]