Follow @devglan

spring Interview Questions and Answers

Each spring Interview Question Sets contains 12 questions.

1)   What is Spring Framework? What are it’s main modules?

The Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure part so you can focus on your application part.Currently it consists of five major modules divided again different submodules These are follows.
Core Container -The Core Container consists of the Core, Beans, Context, and Expression Language modules
Data Access/Integration- The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction modules
Web-The Web layer consists of the Web, Web-Servlet, Web-Struts, and Web-Portlet modules.
AOP and Instrumentation-Spring's AOP module provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, method-interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated
Test -The Test module supports the testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring ApplicationContexts and caching of those contexts.
It also provides mock objects that you can use to test your code in isolation.

2)   What are the benefits of Spring Framework?

Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.
Inversion of control (IOC): Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.
Aspect oriented (AOP): Spring supports Aspect oriented programming and separates application business logic from system services.
Container: Spring contains and manages the life cycle and configuration of application objects.
MVC Framework: Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks.
Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).
Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.

3)   What is a core container in Spring?

A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.
It is called the heart of spring (Core Container) as it contains the IOC Container.

4)   What is IOC in Spring?

IOC is a design pattern followed to achieve loose coupling between application components.Spring uses this pattern to achieve it. It is named as Inversion of control as we are taking backing the control from dependent components and throwing it to some container.

5)   What is the benefits of following IOC pattern?

1 : Your code gets decoupled so you can easily exchange implementations of an interface with alternative implementations
2 : It is a strong motivator for coding against interfaces instead of implementations
3 : It's very easy to write unit tests for your code because it depends on nothing else than the objects it accepts in its constructor/setters and you can easily initialize them with the right objects in isolation.

6)   What is ApplicationContext in Spring?

The ApplicationContext is the central interface within a Spring application for providing configuration information to the application.
The ApplicationContext provides:
Bean factory methods for accessing application components.
The ability to load file resources in a generic fashion.
The ability to publish events to registered listeners.
The ability to resolve messages to support internationalization.
provides Inheritance support from a parent context.

7)   What is the significance of BeanFactory interface in Spring?

BeanFactory helps in creating Spring object with some basic functionality around object management through a well defined configuration framework.
Creating a BeanFactory from xml is as follows
: BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));

8)   What is the difference between Beanfactory and ApplicationContext in Spring?

BeanFactory: It Instantiates beans lazily
It has no support for Message Internationalization.
It has no event handling or event propagation mechanism.
ApplicationContext: Beans are instantiated when ApplicationContext is loaded/up.
It supports Message Internationalization
It provides event-propagation to the beans which are participating in listening to ApplicationContext
i.e. the beans implementing ApplicationListener interface.

9)   What are the common implementations of the Application Context ?

There are three most commonly used implementation of ApplicationContext as:
ClassPathXmlApplicationContext : It loads the context definition from an XML file present in the classpath, treating context definitions as classpath resources. File ‘beans.xml’ should be present in classpath of application.
ApplicationContext appContext = new ClassPathXmlApplicationContext(“beans.xml”);
FileSystemXmlApplicationContext : It loads the context definition from an XML file present in the filesystem. Need to give absolute path of the file ‘beans.xml’ as given below :
ApplicationContext appContext = new FileSystemXmlApplicationContext(“F:/java_program/SpringProject/src/com/mycontext/beans.xml”);
XmlWebApplicationContext : XmlWebApplicationContext is used to represent Spring container for web applications.It loads bean definitions from more than one xml Files.
we can specify their locations in contextConfigLocation parameter of ContextLoaderListener in web.xml file.

10)   What is a spring Bean?

Any normal java class that is initialized by Spring IOC container is called Spring Bean. Spring uses ApplicationContext to get the Spring Bean instance.

11)   What is lifecycle of a bean in Spring?

Spring container performs the following to get a qualified bean.Those steps form the lifecycle of a bean 1: The spring container finds the bean’s definition from the XML file and instantiates the bean.
2:Using the dependency injection, spring populates all of the properties as specified in the bean definition.
3:The factory calls setBeanName() passing the bean’s ID if the bean implements BeanNameAware interface
4:The factory calls setBeanFactory(), passing an instance of itself if the bean implements BeanFactoryAware interface
5:If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.
6:If an init-method is specified for the bean, it will be called.
7:Finally postProcessAfterInitialization() methods will be called if any BeanPostprocessors associated with it.

12)   What are the scopes of spring Bean?

singleton: Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure spring bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues because it’s not thread-safe.
prototype: A new instance will be created every time the bean is requested.
request: This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
session: A new bean will be created for each HTTP session by the container.
global-session: This is used to create global session beans for Portlet based applications.
Spring Framework is extendable and we can create our own scopes too, however most of the times we are good with the scopes provided by the framework.

Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.