Spring
Spring
Questions,
Q1. What are the major features in different versions of Spring Framework?
Q2. What is a Spring Framework?
Q3. List the advantages of Spring Framework
Q4. What are the different features of Spring Framework?
Q5. How many modules are there in Spring Framework and what are they?
Q6. What is a Spring configuration file?
Q7. What are the different components of a Spring application?
Q8. What are the various ways of using Spring Framework?
Q9. What is Spring IOC Container?
Q10. What do you mean by Dependency Injection?
So, here are the Top 50 Spring Interview Questions which are most likely to be asked by the
interviewer. If you are seeking a future in this field, these questions will surely help you to ace the
interview. For your ease of access, I have categorized the questions under a few topics, namely:
General Questions
Dependency Injection/ IoC
Spring Beans
Spring Annotations
Spring Data Access
Spring AOP
Spring MVC
This version was released in 2013. This was the first version
Spring 4.0
to provide full support to Java 8.
5. How many modules are there in Spring Framework and what are they?
There are around 20 modules which are generalized into Core Container, Data Access/Integration, Web,
AOP (Aspect Oriented Programming), Instrumentation and Test.
Spring Core Container – This layer is basically the core of Spring Framework. It contains the
following modules :
a. Spring Core
b. Spring Bean
c. SpEL (Spring Expression Language)
d. Spring Context
Data Access/Integration – This layer provides support to interact with the database. It contains
the following modules :
Web – This layer provides support to create web application. It contains the following modules :
a. Web
b. Web – MVC
c. Web – Socket
d. Web – Portlet
Aspect Oriented Programming (AOP) – In this layer you can use Advices, Pointcuts etc., to
decouple the code.
Instrumentation – This layer provides support to class instrumentation and classloader
implementations.
Test – This layer provides support to testing with JUnit and TestNG.
Messaging – This module provides support for STOMP. It also supports an annotation
programming model that is used for routing and processing STOMP messages from WebSocket
clients.
Aspects – This module provides support to integration with AspectJ.
Spring Framework can be used in various ways. They are listed as follows:
The next section of Spring Interview Questions is on Dependency Injection and IoC container.
Powered by Edureka
At the core of the Spring Framework, lies the Spring container. The container creates the object, wires
them together, configures them and manages their complete life cycle. The Spring container makes use
of Dependency Injection to manage the components that make up an application. The container receives
instructions for which objects to instantiate, configure, and assemble by reading the configuration
metadata provided. This metadata can be provided either by XML, Java annotations or Java code.
Constructor Injection
Setter Injection
Interface Injection
BeanFactory vs ApplicationContext
BeanFactory ApplicationContext
It is an interface defined in It is an interface defined in
org.springframework.beans.factory.BeanFacto org.springframework.context.ApplicationConte
ry xt
It uses Lazy initialization It uses Eager/ Aggressive initialization
It explicitly provides a resource object using the It creates and manages resource objects on its
syntax own
It doesn’t supports internationalization It supports internationalization
It doesn’t supports annotation based
It supports annotation based dependency
dependency
15. List some of the benefits of IoC.
Some of the benefits of IoC are:
Let’s move on to the next section of Spring Interview Questions, that is Spring Beans Interview Questions.
XML-Based configuration: In Spring Framework, the dependencies and the services needed by
beans are specified in configuration files which are in XML format. These configuration files
usually contain a lot of bean definitions and application specific configuration options. They
generally start with a bean tag. For example:
Annotation-Based configuration: Instead of using XML to describe a bean wiring, you can
configure the bean into the component class itself by using annotations on the relevant class,
method, or field declaration. By default, annotation wiring is not turned on in the Spring container.
So, you need to enable it in your Spring configuration file before using it. For example:
1 <beans>
<context:annotation-config/>
2
<!-- bean definitions go here -->
3 </beans>
4
Singleton: This provides scope for the bean definition to single instance per Spring IoC
container.
Prototype: This provides scope for a single bean definition to have any number of object
instances.
Request: This provides scope for a bean definition to an HTTP-request.
Session: This provides scope for a bean definition to an HTTP-session.
Global-session: This provides scope for a bean definition to an Global HTTP-session.
The last three are available only if the users use a web-aware ApplicationContext.
19. What is the Bean life cycle in Spring Bean Factory Container?
Bean life cycle in Spring Bean Factory Container is as follows:
1. The Spring container instantiates the bean from the bean’s definition in the XML file.
2. Spring populates all of the properties using the dependency injection, as specified in the bean
definition.
3. The factory calls setBeanName() by passing the bean’s ID, if the bean implements the
BeanNameAware interface.
4. The factory calls setBeanFactory() by passing an instance of itself, if the bean implements the
BeanFactoryAware interface.
5. preProcessBeforeInitialization() methods are called if there are any BeanPostProcessors
associated with the bean.
6. If an init-method is specified for the bean, then it will be called.
7. Finally, postProcessAfterInitialization() methods will be called if there are any
BeanPostProcessors associated with the bean.
To understand it in better way check the below diagram:
Student.java
1
2 public class Student
{
3 private Person person;
4 //Setters and Getters
5 }
6 public class Person
7 {
private String name;
8 private String address;
9 //Setters and Getters
10 }
11
studentbean.xml
22. What do you understand by auto wiring and name the different
modes of it?
The Spring container is able to autowire relationships between the collaborating beans. That is, it is
possible to let Spring resolve collaborators for your bean automatically by inspecting the contents of the
BeanFactory.
Different modes of bean auto-wiring are:
a. no: This is default setting which means no autowiring. Explicit bean reference should be used for
wiring.
b. byName: It injects the object dependency according to name of the bean. It matches and wires
its properties with the beans defined by the same names in the XML file.
c. byType: It injects the object dependency according to type. It matches and wires a property if its
type matches with exactly one of the beans name in XML file.
d. constructor: It injects the dependency by calling the constructor of the class. It has a large
number of parameters.
e. autodetect: First the container tries to wire using autowire by constructor, if it can’t then it tries to
autowire by byType.
1 @Configuration
2 public class AnnotationConfig
3 {
4 @Bean
5 public MyDemo myDemo()
{ return new MyDemoImpll(); }
6 }
7
25. How annotation wiring can be turned on in Spring?
By default, Annotation wiring is not turned on in the Spring container. Thus, to use annotation based
wiring we must enable it in our Spring configuration file by configuring <context:annotation-
config/> element. For example:
@Component: This marks a java class as a bean. It is a generic stereotype for any Spring-managed
component. The component-scanning mechanism of spring now can pick it up and pull it into the
application context.
@Controller: This marks a class as a Spring Web MVC controller. Beans marked with it are
automatically imported into the Dependency Injection container.
@Service: This annotation is a specialization of the component annotation. It doesn’t provide any
additional behavior over the @Component annotation. You can use @Service over @Component in
service-layer classes as it specifies intent in a better way.
Powered by Edureka
@Repository: This annotation is a specialization of the @Component annotation with similar use and
functionality. It provides additional benefits specifically for DAOs. It imports the DAOs into the DI container
and makes the unchecked exceptions eligible for translation into Spring DataAccessException.
27. What do you understand by @Required annotation?
@Required is applied to bean property setter methods. This annotation simply indicates that the affected
bean property must be populated at the configuration time with the help of an explicit property value in a
bean definition or with autowiring. If the affected bean property has not been populated, the container will
throw BeanInitializationException.
For example:
1
public class Employee
2 {
3 private String name;
4 @Required
5 public void setName(String name)
6 {this.name=name; }
public string getName()
7 { return name; }
8 }
9
28. What do you understand by @Autowired annotation?
The @Autowired annotation provides more accurate control over where and how autowiring should be
done. This annotation is used to autowire bean on the setter methods, constructor, a property or methods
with arbitrary names or multiple arguments. By default, it is a type driven injection.
For Example:
1
public class Employee
2 {
3 private String name;
4 @Autowired
5 public void setName(String name)
6 {this.name=name; }
public string getName()
7 { return name; }
8 }
9
29. What do you understand by @Qualifier annotation?
When you create more than one bean of the same type and want to wire only one of them with a property
you can use the @Qualifier annotation along with @Autowired to remove the ambiguity by specifying
which exact bean should be wired.
For example, here we have two classes, Employee and EmpAccount respectively. In EmpAccount, using
@Qualifier its specified that bean with id emp1 must be wired.
Employee.java
1
2 public class EmpAccount
{
3 private Employee emp;
4 @Autowired
5 @Qualifier(emp1)
6 public void showName()
{
7
System.out.println(“Employee name : ”+emp.getName);
8 }
9 }
10
30. What do you understand by @RequestMapping annotation?
@RequestMapping annotation is used for mapping a particular HTTP request method to a specific class/
method in controller that will be handling the respective request. This annotation can be applied at both
levels:
a. JdbcTemplate
b. SimpleJdbcTemplate
c. NamedParameterJdbcTemplate
d. SimpleJdbcInsert
e. SimpleJdbcCall
34. What are the ways by which Hibernate can be accessed using
Spring?
There are two ways by which we can access Hibernate using Spring:
The next section of Spring interview questions discusses on Spring AOP Interview Questions.
a. Before: These types of advices execute before the joinpoint methods and are configured
using @Before annotation mark.
b. After returning: These types of advices execute after the joinpoint methods completes executing
normally and are configured using @AfterReturning annotation mark.
c. After throwing: These types of advices execute only if joinpoint method exits by throwing an
exception and are configured using @AfterThrowing annotation mark.
d. After (finally): These types of advices execute after a joinpoint method, regardless of the
method’s exit whether normally or exceptional return and are configured using @After annotation
mark.
e. Around: These types of advices execute before and after a joinpoint and are configured using
@Around annotation mark.
42. Point out the difference between concern and cross-cutting concern
in Spring AOP?
The concern is the behavior we want to have in a particular module of an application. It can be defined as
a functionality we want to implement.
The cross-cutting concern is a concern which is applicable throughout the application. This affects the
entire application. For example, logging, security and data transfer are the concerns needed in almost
every module of an application, thus they are the cross-cutting concerns.
Powered by Edureka
44. What are the difference between Spring AOP and AspectJ AOP?