Got an upcoming interview related to Spring Framework? Perhaps you have landed on this page because you are hunting the top Spring Framework interview questions. In our blog, we shall discuss the most asked Spring interview questions and answers.
If you go through these, chances are you will perform incredible at your interview. Before we kickstart, let us give you a brief on the overall demand for Spring Framework in the current market.
Spring makes the Java programming language faster, simpler, and secure for all. Its primary focus is speed, yet it is simple and productive. Due to this, it has become one of the globe’s most popular Java frameworks. The libraries are flexible and reliable. It is used in a lot of places like online shopping, connected cars, and more innovative technologies.
It also brags about its contributions to big giants like Amazon, Google, and Alibaba. Furthermore, the Spring community is huge. It comprises all age groups and is very diverse. You will find great resources and support online to move ahead to the next level making your journey easy.
If you are a Java programmer, then Spring Framework is crucial to you. A lot of companies do require the programmer to understand Spring Framework or at least know some of its modules.
Here are some top Spring interview questions and answers you can expect your interviewer to ask.
Table of Contents
Spring Interview Questions & Answers 2020
Q.1 What are the different versions you can find of Spring Framework, and what are their major features?
The three versions are Spring 2.5, Spring 3.0, and Spring 4.0.
Spring 2.5 was the first version to support annotations. It was released in the year 2007. Spring 3.0 utilized upgrades in Java5 and furthermore offered help to JEE6. It was released two years later in 2009. Spring 4.0 was the main variant to offer comprehensive support to Java 8. It was released in the year 2013.
Q.2 Describe or define Spring Framework
It is an open-source Java platform. It is used as an option for heavy enterprise Java technologies. To put it in simple terms, it simplifies Java development because it is lightweight. Developers can develop applications using Spring Framework and deploy even on computers with restricted CPU memory and resources. Further, it offers support to various other frameworks including but not limited to Tapestry, JSF, EJB, Hibernate, Struts, and more.
Q.3 What are the advantages of the Spring Framework?
- Spring Framework is an open-source and does not have any vendor lock-in.
- You can use only what you really need because of the layered architecture it uses.
- Spring Framework uses POJOs, which makes application testing much easier.
- You can use it with all other frameworks of Java with Spring Frameworks. It does not impose any restrictions or limitations.
- Spring Framework is a modular framework. It is very flexible.
Q.4 What are the various features of the Spring Framework?
- Lightweight: Spring Framework is lightweight when we talk about its transparency and size.
- Inversion of control: It is the principle where the control flow of a program is flipped. Here, the external sources like services, framework, and other components control the flow of a program instead of the programmer.
- Aspect-oriented Programming: Aspect-oriented programming in Spring supports binding development by splitting system services from application business logic.
- Container: The container is at the core of the Spring Framework It is the container that creates the objects, binds them, configures them, and be in charge of their complete life cycle, which is from creation until the end.
- MVC Framework: The Spring Framework’s MVC web application framework is easy to use, and it is configurable. You can use other frameworks instead of picking the Spring MVC Framework.
- Transaction Management: Spring Framework uses a generic abstraction layer for transaction management. You can use Spring’s transaction support in container fewer environments.
- JDBC Exception Handling: You do not need to handle any database-related exceptions explicitly. Spring JDBC Framework handles it using the JDBC exception handling.
Q.5 How many modules can you find in Spring Framework? Name them.
You can find twenty modules which are categorized into Core Container, Web, Data Access/Integration, AOP (Aspect Oriented Programming), Instrumentation, and Test.
Spring Core Container: As the name suggests, it is the core container of Spring Framework. It comprises of
- Spring Core
- Spring Bean
- Spring Expression Language
- Spring Context
Web: This layer gives support to create a web application. It comprises of the following modules:
- Web – MVC
- Web – Socket
- Web – Portlet
Data Access/Integration: This layer gives support to communicate with the database. It comprises of the following modules:
- Java DataBase Connectivity – JDBC
- Object Relational Mapping – ORM
- Object XML Mappers – OXM
- Java Messaging Service – JMS
Aspect-Oriented Programming: In this layer, developers can use pointcuts, advice, etc. to decouple the code.
Instrumentation: This layer gives support to classloader implementations and class instrumentation.
Test: This layer gives support to testing with TestNG and Junit.
You can also find other miscellaneous nodes like:
Messaging: This module offers assistance for STOMP. It also aids as an annotation programming model that is required in routing and processing STOMP messages from WebSocket clients.
Aspects: This module offers support to integrate with AspectJ.
Q.6 What is a Spring configuration file?
It is basically an XML file. You can find all the information about the classes in this file. It further describes how these classes are configured and known to each other. The XML configuration files are detailed, so it is necessary to keep them clean. Otherwise, managing them becomes difficult if your project is big.
Q.7 Describe the different components of a Spring application.
Typically, a Spring application comprises of the following components:
- Interface: All the functions are defined in the interface.
- Bean class: You can find properties here along with the set and get functions.
- Spring Aspect-Oriented Programming (AOP): It gives the functionality of crosscutting concerns.
- Bean Configuration File: This file contains the details of classes and how one can configure them.
- User program: A user program uses the function.
Q.8 Explain the various ways of using the Spring Framework
Spring Framework can be used in several ways like:
- Spring Framework can be used as a comprehensive Spring web application.
- Spring Framework can be used as a third-party web framework, using Spring Frameworks middle-tier.
- Spring Framework can be used for remote usage.
- Spring Framework can be used as Enterprise Java Bean, which can wrap existing POJOs.
Q.9 What is the Spring IOC Container?
The container is at the core of the Spring Framework. It is the container that creates the objects, binds them, configures them, and be in charge of their complete life cycle, which is from creation until the end.
The Spring container uses the Dependency Injection to manage the components that create an application. The container gets directions for which objects to create, set, and call by reading the configuration metadata given either by Java annotations, Java code or by an XML.
Q.10 What is a Dependency Injection?
In Dependency Injection, instead of creating objects, you simply describe them on how to create it. Developers need to describe which services are required by a particular component in the configuration file. There is no need to connect the services to components in the code directly. They are wired by together by the Spring IoC container.
Q.11 In how many methods can one do Dependency Injection?
There are three ways Dependency Injection is done like:
- Constructor Injection
- Setter Injection
- Interface Injection
However, in Spring Framework, only constructor injection and setter injections are used.
Q.12 What makes constructor injection different from setter injection.
|Constructor Injection||Setter Injection|
|There is no incomplete or limited injection.||There can be incomplete or limited injection.|
|It doesn’t override the setter property.||It overrides the constructor property.|
|It creates a new instance if any change is done.||It does not create any new instance if a change is made.|
|It works better for a lot of properties.||It works better for a fewer properties.|
Q.13 How many kinds of IOC containers are available in Spring?
You can typically find two IOC containers, namely:
- BeanFactory: BeanFactory is a sort of a factory class that comprises a group of beans. It instantiates the bean whenever demanded by the clients.
- ApplicationContext: The ApplicationContext interface is created above the BeanFactory interface. It offers some extra functionality, besides the one provided by the BeanFactory.
Q.14 Tell us about a few differences between BeanFactory and ApplicationContext.
|It is an interface explained in org.springframework.beans.factory.BeanFactory||It is an interface explained in org.springframework.context.ApplicationContext|
|It uses Lazy initialization||It uses Aggressive initialization|
|It explicitly gives a resource object using the syntax||It builds and manages resource objects on its own|
|It doesn’t aid internationalization||It aids internationalization|
|It doesn’t aid annotation-based dependency||It aids annotation-based dependency|
Q.15 What are a few benefits of IoC?
Some of the benefits of IoC are:
- It will cut down the amount of code needed in your application.
- Your application is way easier to test because it doesn’t need any singletons or JNDI lookup mechanisms in your unit test cases.
- It encourages loose coupling with minimal effort and the least intrusive mechanism.
- It aids eager instantiation and lazy loading of the services.
Q.16 What are Spring Beans?
Spring Beans are the objects that serve as the backbone of the user’s application. Beans are controlled by the Spring IoC container. By controlling it means, they are instantiated, configured, wired, and managed by a Spring IoC container. Beans are created using the configuration metadata that the users provide to the container.
Q.17 How is configuration metadata provided to the Spring container?
Configuration metadata can be given to Spring container in one of the ways:
- Annotation-Based configuration: One can use annotations on the relevant class to describe the bean wiring into the component class itself. It is not the default setting. So, if you wish to use it, you would have to enable it in the Spring configuration file before one can use it.
- XML-Based Configuration: The dependencies and the services required by beans are mentioned in configuration files. These are in an XML format. They comprise of many bean definitions and application-specific configuration options. They generally begin with a bean tag.
- Java-based configuration: The primary features in Spring Framework’s new Java-configuration support are @Bean annotated methods, and @Configuration annotated classes.
Q.18 How many bean scopes does Spring support?
It supports five scopes which are:
- Singleton: This gives scope for the bean definition to a single instance per Spring IoC container.
- Prototype: This gives scope for a single bean definition to have any number of object instances.
The below scopes are available only if the users employ a web-aware ApplicationContext.
- Request: This gives scope for a bean definition to an HTTP-request.
- Session: This gives scope for a bean definition to an HTTP-session.
- Global-session: This gives scope for a bean definition to a Global HTTP-session.
Q.19 Explain the Bean life cycle in Spring Bean Factory Container
- The Spring container first instantiates the bean from the bean’s definition in the XML file.
- Spring using the dependency injection populates all of the properties.
- The factory calls setBeanName() by giving the bean’s ID if the bean executes the BeanNameAware interface.
- The factory calls setBeanFactory() by creating an instance of itself if the bean executes the BeanFactoryAware interface.
- If there are any BeanPostProcessors related to the bean, then preProcessBeforeInitialization() methods are called.
- If the bean specifies the init method, it is then called.
- If there are any BeanPostProcessors related to the bean, then preProcessAfterInitialization() methods are called.
Q.20 Explain Bean Wiring
Bean wiring is when all the beans are combined together inside the Spring container. The Spring container must understand what beans are required and how it should employ the dependency injection to link the beans together.
Q.21 What is auto wiring and name the different modes of auto wiring?
The Spring container has the capability to auto-wire relationships between the connecting beans. The Spring can automatically resolve collaborators for your bean by seeing the contents of the BeanFactory.
The different modes of bean auto-wiring are:
- no: This is the pre-selected option, which means no auto wiring. You need to use an explicit bean reference for wiring.
- byName: It injects the object dependency based on the name of the bean. It pairs and wires its properties with the beans explained by the exact names in the XML file.
- byType: As the name says, it injects the object dependency according to type. It matches and wires a property if the type matches with one of the names of the beans in an XML file.
- Constructor: A constructor injects the dependency by calling the constructor of the class. It has many parameters.
- Autodetect: By default, the container auto wires using a constructor. If it fails, it then tries to auto-wire by byType.
Q.22 What are the restrictions or limitations of auto wiring?
There are a couple of limitations/restrictions one faces using auto wiring like:
- Overriding possibility: By using the <constructor-arg> and <property> settings, you can specify dependencies. It will override auto wiring.
- Primitive data type: Primitive data type, classes, and strings cannot be auto wired.
- Complicated nature: Explicit wiring is preferred over auto wiring because the latter is confusing in nature.
Q.23 What is Annotation-based container configuration?
An alternative to XML setups, annotation-based container configuration, is used to describe a bean wiring. The developer transfers the configuration into the component class by using annotations on the method, class, or field declaration.
Q.24 How do you turn on annotation wiring in Spring?
Annotation wiring is turned off by default in the Spring container. So, to use annotation-based wiring, you must enable it in the Spring configuration file by setting the <context:annotation-config/> element.
Q.25 Explain the difference between various annotations like @Controller, @Component, @Repository & @Service in Spring?
@Controller: This indicates a class as a Spring Web MVC controller. It automatically imports the beans marked with it into the Dependency Injection container.
@Component: This indicates a java class as a bean. It is a general stereotype for any Spring-managed component. The component-scanning process of Spring can pick it up and pull it into the application context.
@Service: This annotation is an area of component annotation. There is no new behavior over the @Component annotation. One can use @Service instead of the @Component in service-layer classes because it specifies intent appropriately.
@Repository: This annotation is an area of the @Component annotation. It comes with the same functions and uses. However, it offers some extra advantages, precisely for DAOs. It gets the DAOs into the DI container and qualifies the unchecked exceptions for translation into Spring DataAccessException.
Q.26 Describe Spring DAO support
The Data Access Object support in Spring makes it straightforward to access technologies like Hibernate, JDBC, or JDO in a proper and stable way. It enables one to select any of the technologies without any hassle. It also enables one to code without stressing out about catching exceptions that are related to each of this technology.
Q.27 Which classes are available in spring JDBC API?
Classes that are present in the JDBC API are:
Q.28 What are the methods by which one can access Hibernate using Spring?
There are two methods by which one can access Hibernate using Spring:
- The Inversion of Control method with a Callback and Hibernate Template.
- Applying an AOP Interceptor node and Extending HibernateDAOSupport.
Q.29 Name the types of transaction management supported by Spring
Spring supports two types of transaction management. They are:
- Programmatic transaction management: Here, the transaction is controlled with the help of programming. It provides a great amount of flexibility. However, it is hard to maintain.
- Declarative transaction management: Here, transaction management is kept apart from the business code. One can use only annotations or XML based configurations to control the transactions.
Q.30 Describe AOP
AOP is known as Aspect-arranged programming. It is a programming system that lets software engineers modularize crosscutting concerns or conduct that cuts over the standard divisions of obligation. Examples of cross-cutting concerns are logging and transaction management. The core of AOP is an aspect. It exemplifies practices that can influence various classes into reusable modules.
Q.31 What do you mean by Aspect?
Aspect is the modularization of concern, which cuts across multiple objects. A good example of crosscutting concerns in J2EE would be transaction management applications. Aspects are performed utilizing regular classes, or regular classes explained with the @Aspect annotation in Spring Framework.
Q.32 What is a JoinPoint
A point at which the execution of a program is called JoinPoint, for example, the handling of an exception or execution of a method. In Spring AOP, a JoinPoint consistently represents a method execution.
Q.33 What do you mean by the Spring MVC framework?
The Spring web MVC framework offers model-view-controller architecture and ready to use components. You can use these components to create flexible and loosely coupled web apps. The MVC pattern supports in isolating the different aspects of the application like business logic, input logic, and the UI logic while giving a loose coupling between every one of these components.
Q.34 Describe DispatcherServlet
This is the core of the Spring Web MVC framework. It manages all the HTTP requests and responses. The DispatcherServlet gets from the configuration file the entry of handler mapping and passes the request ahead to the controller. The controller then gives an object of Model and View. The DispatcherServlet marks the entry of view resolver in the configuration file and calls the defined view component.
Q.35 Explain WebApplicationContext
The WebApplicationContext is an augmentation of the plain ApplicationContext. It has some more features that are needed for web applications. It is different from a normal ApplicationContext because it has the capability of resolving themes and deciding which servlet it is linked with.
Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
We have covered most of the top Spring interview questions that are asked by the interviewer. If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s Executive PG Program Full-Stack Software Development.
What is Java Spring?
Java Spring developed by Sun Microsystems, is a platform that supports application development. Spring is a collection of Java based services and API's which helps in writing scalable applications. Spring is an open-source application development framework. It can be used with any Java based application, hence making it open source. Spring provides utility classes, exception handling, AOP, and interfaces to support enterprise applications. Java Spring is a platform which supports Java based server-side applications and simplifies the overall development process. It is a highly flexible platform which can be used with any Java based server. It provides utility classes and exception handling services. It has a transactional support system and provides easy binding with databases.
What is Spring MVC?
Spring MVC (Model View Controller) is an open-source web application framework that is written in Java and has been developed over the last four years by a team at SpringSource. It's developed on top of the Servlet API and is available for Java EE 5 Web applications and for Google Web Toolkit applications. It also integrates with Spring Data and Spring AOP, so you can use it with Hibernate, JPA, and aspect-oriented programming, among other frameworks. Spring MVC is highly configurable and supports Spring's Inversion of Control (IoC) principles. It supports the building of web applications that are stateful, so that you can maintain application state information between requests.
What is Spring Hibernate?
Hibernate is an object-relational mapping (ORM) library for Java. Hibernate is designed to handle the Persistence of Java class objects. It is a powerful, lightweight, and extremely portable ORM framework. Hibernate is available under the GNU Lesser General Public License (LGPL). Spring Data is a project within the Spring Framework providing a set of components to access and manage data in a more object-oriented fashion using the familiar Spring Inversion of Control (IoC) paradigm. Hibernate is just an ORM framework. Spring Data on the other hand, is an effort to create an abstraction layer over various storage mechanisms like Relational Database, NoSQL, Flat files etc.