Top Spring Interview Questions

Spring Framework is most popular framework these days and various organizations use this framework.  As per my experience and exposure I have prepared some of them. In many cases they start with Core Java Questions first then further ask from other topics like Spring,Hibernate etc.

  1. What is Spring Framework? Why is it so popular these days?
    The Spring Framework is an application framework and inversion of control container for the Java platform. The core module of Spring Framework is IOC which manages dependency injections. Spring Framework provides several modules for various purpose like Spring MVC,Spring Batch,Spring Data Jpa,Spring WebFlux etc.
  2. What is Spring IOC? What do you mean by dependency injection?
    The Spring IOC container is the core of the Spring Framework. It create the objects, injects dependencies and manage their life cycle. These objects are called Spring Beans.  As you know object creation costs much and also takes effort. Spring IOC promotes the programmer to think about the business logic only and itself managing the object creation things.
  3. What is Spring Bean?
    The POJO classes for which Spring IOC container manages the life cycle and injects dependencies are called Spring Bean.
  4. What are the modules of Spring Framework?
  5. How to define a Spring Bean? 
    We can define Spring Bean by using @Bean annotation or through <bean> element.

    public Student getStudentBean(){
    return new Student();
  6. What are Spring Bean Scope?
    Spring Bean scope lets avail more granular control over the bean instances creation. Spring Bean Scope decides the kind of Spring Bean and their life span. Spring Bean Scope can be singleton,prototype,request,session,global-session.

    @Scope(value ="prototype")
    public Student getStudentBean(){
    return new Student();
  7. What is the default scope of Spring Bean?
    By default Spring create singleton bean. You have to specify the scope for other type.
  8. Difference between BeanFactory and ApplicationContext?
     interface is basic IOC container whereas ApplicationContext  advanced one, which extended the BeanFactory and added the some other features like internalization, expression language etc.
  9. What are the ways by which dependency can be injected?
    There are two ways by which dependencies can be injected either using Setter or Constructor. For optional dependency we use Setter Injection.
  10. What is @Autowired Annotation?
    @Autowired annotation is used for inject dependency. By default dependency injects use by type, so in the case of more than one bean of same type it throws exception for it. Use @Qualifier for avoiding the ambiguity
  11. What is Spring MVC?
    Spring Web MVC is the web framework built on the Servlet API. It built upon the FrontController Design pattern where DispatcherServlet controls all the requests and responses. It dispatches request to the handlers(controllers) using Handler Mapping. From ViewResolver, it identify the view for the request and send response to the user.
    Read More 
  12. What is Spring Controller? How to define a Spring Controller?
    Spring Controller is handler class which is responsible for process request. DispatcherServlet dispatches incoming request to handler class using Handler Mapping. You can define Spring Controller using @Controller and @RestController.
  13. What is ViewResolver ? Types of View Resolvers?
    ViewResolveris a component which is responsible for addressing view for a particular request. It also enables to render models/data.  Spring enables you to use JSPs, Velocity templates and XSLT views.
  14. Difference between @RestController & @Controller?
    Both the annotations are used to define controller/handler class in Spring MVC application. @RestController is combination of @Controller and @ResponseBody.  In case of @RestController you have to return ModelAndView instead string view name.
  15. Difference @PathVariable vs @RequestParam?
    @PathVariable identifies the pattern that is used in the URI for the incoming request. Let’s look at the request URL:


    Here in /product/{productId}

    , productId is pathvariable and param1 and param2 are request query params.

    public Product getProductById(@RequestParam("param1") Integer param1,@RequestParam("param2") Integer param2){
    //code here
  16. What are important Annotations of Spring MVC?
    @RestController,@Controller,@RequestMapping,@GetMapping,@PostMapping,@Autowired,@ControllerAdvice,@ExceptionHandler etc.
  17. How to handle exception through out the application Spring MVC application?
    There are two ways to handle exceptions in Spring MVC Web Application using @ExceptionHandler and @ControllerAdvice. Using @ExceptionHandler you can add exception handling code within the controller. Whereas from @ControllerAdvice is used for global exception handler. We are not using try catch for handling exceptions.
  18. What is Spring Security?
    Spring Security module is used for managing the authentication and authorization for a web application. Using this module you can restrict the URL per role.
  19. What is Spring Boot?
    Earlier to setup Spring Framework needs lots of configuration and dependencies need to added which was tedious task for naive programmer. Spring Boot comes with concept of starter package in which we don’t need to add too much dependencies to create a Spring Application. There are several starter package for each module like web,security,data etc.
  20. What is Microservice Architecture and why Spring Framework most preferred for it?
    Microservice Architecture comes with new concept of application development where application development breaks down into multiple sub application based on feature. Rather in Monolithic Architecture everything is present in single application. Spring Framework is most popular for creating microservices as it provides effortless way to create and deploy microservices.
  21. What is Spring Data JPA?
    Spring Data Jpa provides abstraction for data persistent and provides simpler interface for interacting with database layer. Earlier developer needs to create DAO layer and handles all the persisting and transaction logic into it. Spring Data JPA provides several interfaces like CrudRepository,JpaRepository having various methods like findById(),findAll() etc. Read More

    public interface ProductRepository extends JpaRepository<Product,Long>{
  22. Difference between @Component,@Service and @Repository
    @Component is generic annotation which is used to mark a class as component. @Service is used to mark a class as service and @Repository is used to mark a DAO class.
  23. What is Spring WebFlux?
    Spring WebFlux comes with the new concept of Reactive Programming. It was added in Spring 5 and became popular among Spring Programmers. A key aspect of reactive applications is the concept of backpressure which is a mechanism to ensure producers don’t overwhelm consumers. It is based on Reactive Streams. There is one other implementation of Reactive Streams is RxJava developed by Netflix.

Leave a Reply