Spring Interview Questions

Collection of frequently asked Spring interview questions.

What are the types of Dependency Injection Spring supports?

Setter Injection:

Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

Constructor Injection:

Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.

What are the benefits of the Spring Framework transaction management ?

Spring Framework supports:
Programmatic transaction management.
Declarative transaction management.
Spring provides a unique transaction management abstraction, which enables a consistent programming model over a variety of underlying transaction technologies, such as JTA or JDBC. Supports declarative transaction management. Provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA. Integrates very well with Spring’s various data access abstractions.

What is BeanFactory in Spring framework?

A BeanFactory is an implementation fo the factory design patterns. The BeanFactory is a root interface for accessing a Spring bean container. This is the basic client view of a bean container; further interfaces such as ListableBeanFactory and ConfigurableBeanFactory are available for specific purposes.
This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. Depending on the bean definition, the factory will return either an independent instance of a contained object. The point of this approach is that the BeanFactory is a central registry of application components, and centralizes configuration of application components (no more do individual objects need to read properties files.
There are several implementations of BeanFactory in Spring. But the most useful is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its bean based on the definitions contained in an XML file.
BeanFactory f = new XmlBeanFactory(new FileInputStream(“beans.xml”));

How many modules are there in Spring framework?  Explain the different modules in Spring ?

Spring comprises of seven modules. They are.

The core container:

The core container provides the essential functionality of the Spring framework. A primary component of the core container is the BeanFactory, an implementation of the Factory pattern. The BeanFactory applies the Inversion of Control (IOC) pattern to separate an application’s configuration and dependency specification from the actual application code.

Spring context:

The Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internalization, validation, and scheduling functionality.

Spring AOP:

The Spring AOP module integrates aspect-oriented programming functionality directly into the Spring framework, through its configuration management feature. As a result you can easily AOP-enable any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application. With Spring AOP you can incorporate declarative transaction management into your applications without relying on EJB components.

Spring DAO:

The Spring JDBC DAO abstraction layer offers a meaningful exception hierarchy for managing the exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code you need to write, such as opening and closing connections. Spring DAO’s JDBC-oriented exceptions comply to its generic DAO exception hierarchy.

Spring ORM:

The Spring framework plugs into several ORM frameworks to provide its Object Relational tool, including JDO, Hibernate, and iBatis SQL Maps. All of these comply to Spring’s generic transaction and DAO exception hierarchies.

Spring Web module:

The Web context module builds on top of the application context module, providing contexts for Web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects.

Spring MVC framework:

The Model-View-Controller (MVC) framework is a full-featured MVC implementation for building Web applications. The MVC framework is highly configurable via strategy interfaces and accommodates numerous view technologies including JSP, Velocity, Tiles, iText, and POI.

What is application Context in Spring framework?

A Spring ApplicationContext is a subinterface of BeanFactory.It is somewhat similar to BeanFactory in terms that both define beans,bind them together and make them available on request.In its functioning and ApplicationContext has following advanced features:
-Resolving Messages, supporting internationalization
-Support for an eventing mechanism, allowing application objects to publish events and they can register to be notified of events optionally.
-Supports generic loading and access of file resources like image files.
-Supports customization of container behavior through automatic recognition of special application-specific or generic, bean definitions.

What are the ORM spring supports?

Spring supports the following ORM’s :
Hibernate
iBatis
JPA (Java Persistence API)
TopLink
JDO (Java Data Objects)
OJB

Explain the Spring’s DataAccessException?

Spring’s org.springframework.dao.DataAccessException extends the extends NestedRuntimeException which in turn extends and RuntimeException. Hence DataAcccessException is a RuntimeException so there is no need to declare it in the method signature.