Q. What is Spring Framework?

A. The Spring Framework is lightweight, non-intrusive framework, providing the tools and facilities to write standalone,testable components for use in many common applications and scenarios.Spring enables you to write Plain Old Java Objects, or POJOs, and then connect them together using dependency injection, through a central core component called the application context.POJOs are classes that have no unnecessary restriction, such as implementing an interface for a persistence framework, or extending an abstract class for use in a web application.

Q. Explain spring MVC?

A. The Model-View-Controller (MVC) pattern is a common approach to working with user interfaces.As its name implies, it divide an application into three parts.

1. Model-It is a representation of the data your application is trying to work with.

2. View-It presents the user with a representation of this model this could simply be text on a screen, or perhaps a more complicated visualization using graphics and animation.

3.Controller-It is responsible for processing any input, which will govern how the model is created, and then it passes this model to an appropriate view.The Spring’s web applications use this pattern to provide a clean separation between these three parts and how they fit together, building a small web application for displaying the results.

Q. What are benefits of Spring?

A.The benefits of Spring are given below-


2.Inversion of control (IOC).

3.Aspect oriented (AOP).


5.MVC Framework.

6.Transaction Management.

7.JDBC Exception Handling.

Q. What are the advantages of Spring framework?

A. The advantages of Spring are as follows-

1.Spring has layered architecture.

2.Spring Enables POJO Programming.

3.Dependency Injection or Inversion of Control Simplifies JDBC.

4.Open source and no vendor lock-in.

Q. Name the packages that are the basis for Spring Framework’s IoC container?

A. The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container.

Q. What are the modules of Spring Framework?

A. Spring framework features organized into modules and they group together into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, Messaging, and Test, as shown in the given below diagram

Spring Framework Modules-javavapor.com

Spring Framework Modules

Core Container

The four modules  creates Core Container-  spring-core, spring-beans, spring-context, and spring-expression (Spring Expression Language).

The fundamental part consists of spring-core and spring-beans modules Including the IoC and Dependency Injection, implementation BeanFactory.

  • The Context (spring-context) module :-Access objects in a framework-style, implementation Application Context, support for internationalization,event propagation, resource loading, EJB, JMX, and basic remoting.
  • The spring-expression moduleIt is extension of unified EL language as  jsp specification 2.1, supports powerful querying and manipulating an object at runtime.


The spring-aop module provides an AOP Alliance-compliant aspect-oriented programming implementation  support for defining, method interceptors and pointcuts to cleanly decouple code and  separate spring-aspects module provides integration with AspectJ.


The spring-instrument module provides class instrumentation support and classloader implementations.


Spring Framework 4 introduce a spring-messaging module with key abstractions from the Spring Integration project like as Message, MessageChannel, MessageHandler and  a set of annotations for mapping messages to methods.

Data Access & Integration

The JDBC, ORM, OXM, JMS, and Transaction modules form Data Access/Integration layer

  • The spring-jdbc module provides a JDBC-abstraction layer that eliminate the need to do tedious JDBC coding and parsing of database-vendor specific error codes.
  • The spring-tx module supports programmatic and declarative transaction management for classes that implement special interfaces and for all POJOs.
  • The spring-orm module provides integration layers for object-relational mapping APIs, including JPA, JDO, and Hibernate.
  • The spring-oxm module provides an abstraction layer that supports Object/XML mapping implementations such as JAXB, Castor, XMLBeans, JiBX and XStream.
  • The spring-jms module contains features for producing and consuming messages.


The spring-web, spring-webmvc, spring-websocket, and spring-webmvc-portlet modules.forms Web layer.

  • The spring-web module provides basic web-oriented integration features.
  • The spring-webmvc module contains Spring’s model-view-controller (MVC) implementation for web applications and separation between domain model code and web forms.
  • The spring-webmvc-portlet module provides the MVC implementation to be used in a Portlet environment and using all the functionality of the spring-webmvc module.


The spring-test module supports the unit testing and integration testing of Spring components with JUnit or TestNG and mock objects that can use to test code in isolation.

Q. What is Dependency Injection?

A. According to the concept, you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (the IOC container) is then responsible for hooking it all up.

It is a process whereby objects define their dependencies only through constructor arguments, arguments to a factory method, or properties. The container then injects those dependencies when it creates the bean.After that this process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes.The decoupling is more effective when objects are provided with their dependencies. The object does not look up its dependencies, and does not know the location or class of the dependencies. As such, your classes become easier to test.

Q. What are the different types of IOC (dependency injection) ?

A. The types of dependency injection are-

1.Constructor Injection : Dependencies are provided as constructor parameters.

2.Setter Injection : Dependencies are assigned through JavaBeans properties.

Q. Give only two scenarios where Dependency Injections are useful?

A. The two scenarios where Dependency Injections are useful-

1.When you inject the same dependency into multiple components.

2.When You inject different implementation of the same dependency.

Q. Can you mix constructor-based or setter-based DI,which would you suggest?

A. Yes,you can mix Constructor-based and Setter-based DI it is a good to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies.

Q. What is Spring configuration file?

A. Spring configuration file is an XML file.It contains the classes information and describes how these classes are configured and introduced to each other.

Q. What are the benefits of IOC?

A. The benefits of IOC are given below:-

1.It minimizes the amount of code.

2.It increase more testability of application, since no singletons or JNDI lookup mechanisms are required in unit tests.

3.Loose coupling is promoted with minimal effort and least intrusive mechanism.

4.IOC containers support eager instantiation and lazy loading of services


Q. What are the common implementations of the ApplicationContext?

A. The three commonly used implementation of Application Context are:-

1.FileSystemXmlApplicationContext: This Container require for loading beans definition from xml file by passing full path of specified file sytem to the constructor.

2.ClassPathXmlApplicationContext:This container require for loading beans definition from xml file by passing classpath to the constructor.

3.WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.

Q. What are Spring beans?

A.In Spring, components or objects are called beans or In other words,the objects that form the backbone of an application and are managed by the Spring IoC container are called beans.A bean is simply an object that is instantiated, assembled and otherwise managed by a Spring IoC container.Beans are object instances that would be created by the spring framework by looking at their class definitions. These definitions basically form the configuration metadata. The framework then creates a plan for which objects need to be instantiated, which dependencies need to be set and injected,the scope of the newly created instance, etc., based on this configuration metadata.The metadata can be supplied in a simple XML file for example, in the form of XML definitions.

Example shows the simple XML file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<bean name="myBean" class="com.javavapor.beans.MyBean">
<constructor-arg value="MyConstructorArgument"/>
<property name="property1" value="value1"/>
<property name="otherBean" ref="myReferenceBean"/>

Q. What type of parameter provides from bean through metadata to a Spring container?

A. The bean definition contains the information called configuration metadata which is needed for the container to know the followings:-

1.A package-qualified class name.

2.A bean behavioral configuration elements.

3.References to other beans which are needed for the bean to do its work.

4.Other configuration settings to set in the newly created object.

The above metadata define beans for managing their lifecycle and depdencies.

Q. Name the methods that provide configuration metadata to the Spring Container?

A. There are following three important methods that provide configuration metadata to the Spring Container-

1.XML based configuration file.

2.Annotation-based configuration.

3.Java-based configuration.

Q. What is Bean scopes?

A. When defining a bean with Spring,specify how the instances created and managed as they’re retrieved from the container. This is called the bean scope.

There are five such scopes: singleton, prototype, request, session, and global session.

Scope is configured with the scope attribute:

<bean id="departmentDao"

Q. What bean scopes does Spring support? Explain them.

A. Beans can be defined to be deployed in one of a number of scopes.The Spring Framework supports five scopes, three of which are available only if you use a web-aware ApplicationContext.

The following scopes are supported out of the box.

Scope Definition

singleton -The Spring container returns a single instance. This is the default value.

prototype -The Spring container returns a new instance every time it is requested.

request -The Spring container returns a new instance on each HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.

session -The Spring container returns a new instance on each HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

globalSession -The Spring container returns a single instance per global HTTP session.Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

application -A single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

Q. Describe the lifecycle of a bean in Spring framework?

A. The lifecycle of a bean in Spring framework is as follows-

• The framework factory loads the bean definitions and creates the bean.

• The bean is then populated with the properties as declared in the bean definitions.

If the property is a reference to another bean, that other bean will be created and populated, and the reference is injected prior to injecting it into this bean.

• If your bean implements any of Spring’s interfaces, such as BeanNameAware or BeanFactoryAware, appropriate methods will be called.

• The framework also invokes any BeanPostProcessor’s associated with your bean for pre-initialzation.

• The init-method, if specified, is invoked on the bean.

• The post-initialization will be performed if specified on the bean

Q. Explain the ‘Hooks Method’along with their types?

A. Spring Framework provides a number of hooks in the form of callback methods. These methods provide opportunity for the bean to initialize properties or clean up resources.

They are of two types-

1. init-method

2. destroy-method.

1. init-method

When the bean is created, a Spring invoke a specific method on bean to initialize. This method provides a chance for bean to do performance tasks and to do some initialization, such as creating data structures, creating thread pools, etc.

In the example, declare the method in the XML file as shown below-

Example init-method declaration

The FileReader class with init-method is provided in below example.

FileReader with init-method

<bean name="fileReader" class="com.javavapor.beans.FileReader"

Once the FileReader is created, its init-method as declared in the config is invoked. The init method in this case creates a List of Locations.


The framework also invokes a destroy method to clean up before destroying the bean. Framework provides a hook with the name destroy-method, as shown below-

public class FileReader implements Reader {
private List<Location> locations = null;
// This method is invoked by the Spring
//Framework before destroying the bean
public void cleanUp(){
locations = null;

mention the cleanUp method as destroy method in the XML declarations shown below.

<bean name="fileReader" class="com.javavapor.beans.FileReader"

When the program quits, the framework destroys the beans.

Q. Explain when to use singleton and prototype scope bean?

A. The singleton scope should be used for stateless beans and the prototype scope for all beans that are stateful.

Q. In what way beans become singleton or prototype?

A. When declaring a bean in Spring,declare the scope for that bean. To produce a new bean instance each time one is needed,declare that bean’s scope attribute to be prototype. On the other hand, when the same instance of a bean must be receive every-time,declare that bean’s scope attribute to be singleton.

Q. What is default scope of bean in Spring framework?

A. The default scope of bean is Singleton for Spring framework.

Q. Are Singleton beans thread safe in Spring Framework?

A. No, singleton beans are not thread-safe in Spring framework.

Visit: Spring AOP Interview Question & Answer