Loading…

Spring Interview questions and Answers

By Umesh Singh
Last update: 25 Apr 2020, 21 Questions
Spring Interview questions

Spring is one of the most popular open-source app development frameworks for enterprise-class Java. This framework enables developers to build robust applications from plain old Java objects (POJOs) and apply enterprise services to POJOs. Hundreds and thousands of developers globally use Spring to create easy to test, high-performing, and reusable code.

Our Spring interview questions for experienced professionals is a huge hit amongst potential candidates looking for new jobs. Have you read them yet?

Spring is lightweight in terms of size. Some of its core features can be used to develop any Java application, and there are in-built extensions on top of the Java EE platform for building web applications. Spring Framework is called a one-stop platform for building enterprise-level Java applications. Reading Spring interview questions before your next interview may significantly enhance your chances of grabbing your next job.

Most Frequently Asked Spring Interview questions

1. What is spring? Explain

Spring is one of the most popular open source app development frameworks for enterprise-class Java. Spring is lightweight in terms of size. Some of its core features can be used to develop any Java application, and there are in-built extensions on top of the Java EE platform for building web applications. Spring Framework is called a one-stop platform for building enterprise-level Java applications.

2. Explain the benefits of Spring framework?

Following are the benefits of using Spring Framework:

  • Enables developers to use POJO to build enterprise-class applications
  • Simpler testing processes
  • MVC framework provides good alternative to other frameworks, like Struts
  • Convenient API translates technology-specific exceptions
  • Lightweight IoC containers
  • Consistent transaction management interface
3. Explain the difference between IOC and Di?
  IOC DI
1. Program delegates someone else to drive the flow Program flow controlled by framework or runtime
2. General parent term Subset of IOC
4. What do you mean by dependency injection and how does it work?

Dependency Injection or DI is a design pattern that implements inversion or change of control for resolving dependencies. It is a process through which objects define their dependencies. Transferring the task of creating objects to someone else and using the dependency is called dependency injection. It can be done in 2 ways: constructor-based DI and setter-based DI.

5. List the modules used in Spring framework?

Spring framework has 7 modules.

  • The Core container module
  • Application context module
  • AOP module (Aspect Oriented Programming)
  • JDBC abstraction and DAO module
  • O/R mapping integration module (Object/Relational)
  • Web module
  • MVC framework module
7. What do you mean by AOP?

Aspect-oriented programming or AOP is a programming approach that allows different properties of a program to determine how they are compiled into a program. You can use AOP with object-oriented programming or OOP.

8. Explain the different types of spring container?

Spring provides 2 types of containers:

  • Spring BeanFactory Container : It provides basic support for DI and is defined by org.springframework.beans.factory.BeanFactory interface. BeanFactory and related interfaces are available in Spring for backward compatibility with third-party frameworks.
  • Spring ApplicationContext Container : It adds enterprise-specific functionalities to interested event listeners. It is defined by org.springframework.context.ApplicationContext interface.
9. What is BeanFactory and ApplicationContext in Spring?

BeanFactory is the container that instantiates, manages and configures beans. Beans usually collaborate with each another and share dependencies, which are shown in the data used by the BeanFactory.

The ApplicationContext is the interface within a Spring application for offering configuration information to the application.

11. What are setter injection and constructor injection in spring?

Setter Injection is a type of dependency injection (DI) in which the framework uses a setter method to inject dependent objects into the client. The container first calls the no-argument constructor and later it calls the setters. Such setter-based injections can work even if dependencies are injected using the constructor.

Constructor injection uses a constructor for injecting dependencies on Spring-managed beans. The constructor DI is achieved when the container initiates a class constructor with arguments, each argument representing dependencies on other classes.

12. What do you mean by Autowiring in spring?

The Autowiring feature enables developers to inject dependencies in the object implicitly. Autowiring internally uses the setter or constructor injection. This feature cannot be used for injecting primitive or string values, and it only works with reference.

13. What do you mean by configuration annotation in spring?

Configuration annotation is a part of the spring framework. It indicates that class includes @Bean definition methods. This helps the container process the class and generate Beans for using in the application and generating definitions and service requests at the runtime.

14. Explain the difference between @autowired and @inject?
  Autowired Inject
1. Spring's legacy annotation Available from Spring 3 onwards
2. Process to figure out dependencies of a bean Standard annotation for DI
3. Contains 'required' attribute Does not contain 'required' attribute
15. What do you mean by @qualifier in spring?

The @Qualifier annotation in Spring is used in cases where the developer has created more beans of the same type but only wants to wire one of them with the said property. The @Qualifier annotation can be used with @Autowired for removing the confusion of specifying and identifying the exact bean to be wired.

16. Explain the difference between Singleton and prototype bean in spring?
  Singleton Prototype
1. Single bean to a single object Single bean to any number of objects
2. Returns same object each time it gets injected Creates a new object each time it gets injected
3. Use for stateless beans Use for stateful beans
17. What do you mean by DAO in Spring?

DAO or Data Access Object is a design pattern where DAO is an object, which provides an abstract interface to a database or some other type of persistence mechanisms. This support aims at working with data access technologies like Hibernate and JPA easy and consistent.

18. Explain the use of Spring Security?

Spring Security is one of the security modules in the Spring framework. Based on the Java SE/Java EE framework, it provides authentication and authorization to web and enterprise applications. This powerful and highly customizable framework is the de-facto standard for securing all spring-based apps.

19. What do you mean by @controller annotation in spring?

@Controller annotation is a specialization of @Component class that allows the implementation class to be detected through classpath scanning. @Controller annotation annotates Classic controllers. @Controller annotation is used in MVC framework in combination with @RequestMapping annotation in request handling.

20. What do you mean by ViewResolver in Spring?

Like all MVC frameworks that provide a way to work with views, Spring offers the same feature through the view resolvers. The view resolvers enable the developers to render models in the browser without the need to implement a specific view method. In ViewResolver, you can map view names with actual views. Some of the view resolvers in the Spring framework are InternalResourceViewResolver, ResourceBundleViewResolver, and XmlViewResolver.

21. List some of the Design Patterns used in the Spring Framework?

Here are some of the design patterns used in Spring:

  • MVC
  • Front controller
  • View Helper
  • Singleton
  • Prototype
  • Factory
  • Builder
  • Template
  • Proxy
  • DI/IOC
Development History of Spring

Written by Rod Johnson initially, Spring framework was released first in June 2003 under the Apache 2.0 license. Spring was developed in response to the highly complex J2EE specifications. Potential employers feel critical information like this should be known to candidates preparing for Spring interview question and answer.

Advantage of Spring
  • Enables developers to use POJO to build enterprise-class applications
  • Simpler testing processes
  • MVC framework provides good alternative to other frameworks, like Struts
  • Convenient API translates technology-specific exceptions
  • Lightweight IoC containers and Consistent transaction management interface
Latest Version

Latest version 5.1.4 was released in Jan 2019