Got an upcoming interview related to Spring Framework? Perhaps you have landed on this page because you are hunting for 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 incredibly at your interview. Before we kickstart, let us give you a brief on the overall demand for Spring Framework in the current market.
Read: Full stack developers demand in India
Spring makes the Java programming language faster, simpler, and more 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.
Check out our free courses to get an edge over the competition
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.
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.
Refer to the below-mentioned pointers to understand the features between all these versions.
- Compatible with 1.4 and higher versions
- It does not support type conversions and field formatting
- It does not fully support JSR303 bean validation API.
- It does not support embedded database engines.
- It does not support Comprehensive REST.
- Compatible with Java 5 and higher versions
- Supports type conversion and field formatting.
- Fully support JSR303 bean validation API.
- Support various embedded database engines such as Derby, H2, etc.
- Available Comprehensive REST support.
- New features compared to Spring 3.0
- Support configuring and implementing Spring-style applications using Groovy 2.
- Support Groovy applications.
These types of questions can be expected in spring interview questions and answers for experienced professionals. While navigating the answer, make sure to put a structure to it that reflects your knowledge and confidence.
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 them 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.
Features of Spring Framework include-
- The primary feature is dependency injection.
- Dependencies are supposed to be defined manually in the pom.xml file
- Server is supposed to be set explicitly for the testing procedure.
- Boilerplate code is supposed to be written for smaller tasks.
- Does not provide any in-memory database.
Spring-related interview questions like such should be answered strategically. The professionals must not suffice their answers with the definition; instead, they extend their response by including features, properties, advantages and more.
Read: Full Stack Project Ideas & Topics
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.
- Flexibility for Configuring Spring.
- No need server
- There is no need to reinvent.
- Use of Spring AOP.
- Ease of testability. Testing with Spring is easier.
- Consistence of transaction management.
- Provides inversion control.
- It is organised in a modular fashion.
Check out upGrad’s Java Bootcamp
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, external sources like services, frameworks, 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 is 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 fewer container 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.
Read: Full Stack Developer Interview Questions
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
Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)
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.
Explore Our Software Development Free Courses
|Blockchain Technology||React for Beginners||Core Java Basics|
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.
The advantages of a Spring configuration file include-
- It contains the information of the classes.
- Helps describe how the classes are configured and introduced to each other.
- They are verbose and cleaner.
- In case of not written correctly, managing more significant projects can become a problem.
Q.7 Describe the different components of a Spring application.
Typically, a Spring application comprises 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.
- It can be used to simplify the complex and burdensome Java application development process by offering technology like Aspect-Oriented-Programming (AOP).
- It can be used to build secure, scalable and robust applications.
- It acts as a base that powers various Spring-based projects such as Spring Boot, Spring GraphQL, Spring Cloud, etc.
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 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 an XML.
Features of Spring IOC Container-
- Acts as a core of Spring Framework
- Provides two types of containers-
- Bean Factory Container
- Application Context Container
- It performs multiple tasks such as creation of object, configutring and assembling their dependencies and most importantly, manage their entire lifecycle.
- To manage the components, the container uses Dependency Injection (DI).
Spring interview questions for 10 years experience or freshers, this question can be asked to any candidate regardless of their experience. Make sure to be well prepared with the answers.
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.
Importance of Dependency Injection-
- It is helpful to develop a code that is testable.
- Easy writing of codes.
- Mock database can be used with Dependency Injection
- Allows the code to be clean and readable.
- It allows for lose coupling of components.
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.|
|There is no partial injection.||There can be partial injection.|
|Dependencies are inserted using constructor method.||Dependencies are inserted using setter methods.|
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.
- It provides configuration and basic functionality.
- It instantiates, configures and manages many number of beans.
- These beans show a personalit of collaboration, thus help in having dependencies between themselves.
- ApplicationContext: The ApplicationContext interface is created above the BeanFactory interface. It offers some extra functionality, besides the one provided by the BeanFactory.
- What objects to instantiate,configure and assemble gets instructed by reading configuration metadata.
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.
- Easy to switch between different implementations of a particular class.
- Increases the modularity of a program.
- Helps in managing the object’s lifecycle and configuration.
- Provides greater flexibility.
Explore our Popular Software Engineering Courses
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.
Features of Spring beans include-
- Creates an object.
- Provide dependencies
- Intercept an object method
Properties of Spring beans include-
- Initialisation mode
- Destruction call back
- Initialisation call back
Spring interview questions like such are very common but important. Make sure to focus your attention on preparing complex topics but do not forget to practice the simple questions.
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.
- Creates exactly one instance of the object that is defined by the bean definition.
- The single instance is stored in the cache.
- The modifications get reflected in all references to the bean.
2. Prototype: This gives scope for a single bean definition to have any number of object instances.
- Prototype scpe should be used in the state-full beans.
- A new bean instance is created everytime a request for a specific bean is made.
In-Demand Software Development Skills
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. There is no need to do anyting specifically for special 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.
Autowiring is wired best when it is used consistently across a project. Autowiring can help in significantly reducing the need to specify properties. But the limitations or disadvantages should also be considered.
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.
It is done by using annotations on the relevant class, or the field of declaration. Before performing the XML,injection annotation is performed.
Some of the important Spring annotations include-
- @ Required
- @ Autowired
- @ Qualifier
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.
Features of DAO support include-
- Allows the programmers to code without worrying about catching exceptions that are specific to each technology.
- Makes it easy to work with technologies like JDBC, Hibernate, JPA or JDO.
- The DAO class is responsible for two concepts like;
- encapsulating the details of the persistent layer.
- Providing a CRUD interface to the single entity.
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. It gives extreme flexibility. It allows the management using the source code.
- 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. It is less flexible. It supports AOP.
upGrad’s Exclusive Software Development Webinar for you –
SAAS Business – What is So Different?
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.
- AOP provides a pluggable way to add the concern dynamically.
- 7 core concepts of Spring AOP include;
- Join point- It is a candidate point in the program execution where an aspect can be plugged in.
- Weaving- It is the process of linking aspect with other application types.
- Proxy- Object that is created after applying advice to the target object.
- Pointcut- Expression matched with join points determining whether advice needs to e executed or not.
- Advice- Specific actions taken for a particular join point.
- Aspect- Class that implements the JEE application concerns cutting through multiple classes.
- Target Object- Objects on which the advices are applied.
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.
Read our Popular Articles related to Software Development
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.