Core Spring Interview Questions

Here are Spring Interview Questions that I could think while using Spring’s core features e.g. dependency injection.

What is Spring?

Spring is an open source enterprise application development framework, which is primarily based on IOC (inversion of control) or DI (dependency injection) design pattern. It provides ready container to create and manage objects and also provides enterprise services to those objects. It provides ready components for different tiers of application e.g. web, middle/business and data access.

spring

What is inversion of control (IOC) or Dependency Injection?

Inversion of control (IOC) or dependency injection (DI) is a design pattern used to give control to the assembler of classes. Generally, if a class wants to use another class, it instantiates desired class. But using this design pattern, the instantiation control is provided to the assembler. Assembler instantiates the required class and injects it in using class.

What are different types of DI?

– Constructor Injection

– Setter Injection

– Interface Injection

What are different modules in Spring?

Following six modules are there in Spring.

– Core: Springs IoC container and core services

– Web: Spring MVC and ability to integrate Spring with other web frameworks like Strusts, Tapestry, JSF etc.

– JEE: Java enterprise services like EJB support, JMX, JMS, JCA etc.

– ORM: Support to integrate with object relation mapping frameworks like hibernate, iBatis, Toplink etc.

– DAO: Helps in implementing Data Access Object design pattern. Provides support for Spring JDBC transaction management.

– AOP: Implementation of cross cutting concerns through Spring AOP and AspectJ.

What is new in Spring 2.5 as compared to 2.0?

Following changes are introduced in Spring 2.5.

– IOC container: New bean scopes, easier xml configuration, extensible xml authoring, annotations

– AOP: Easier xml configuration, support for @AspectJ aspects, support for bean name pointcut element, support for AspectJ load-time waving

– Middle tier: Declarative transactions in xml, full Websphere transaction management support, JPA, Asynchronous JMS, JDBC improvements

– Web tier: Changes in Spring MVC, Portlet framework, Tiles, JSF, JAX-WS support, etc.
What is IoC container of Spring?

Spring IoC container take care of instantiation of objects, injection of objects in each other and providing enterprise services (e.g. AOP, transaction management) to these objects.

What is BeanFactory interface?

BeanFactory provides configuration framework to Spring object creation and basic functionality around object management.

What is ApplicationContext?

ApplicationContext is built around Spring’s BeanFactory and it provides enterprise centric features e.g. AOP features, message resourcing, event propagating, application-layer-specific contexts to applications.

What is difference between BeanFactory and ApplicationContext?

BeanFactory is core configuration and basic functionality centric while ApplicationContext is enterprise-centric functionality support.

What is your preference BeanFactory or ApplicationContext? Why?

ApplicationContext. It provides all features provided by BeanFactory and enterprise centric more features, which may be required by application in future.

How to instantiate IoC container?

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {“services.xml”, “daos.xml”});

How does a web application use Spring’s configuration xmls?

Spring container/configuration xmls can be integrated with web application through web.xml. Following entries in web.xml can integrate Spring container with web container.

	<context-param>
		<description>
		Context parameter to integrate Spring and Web containers
		</description>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		classpath: services.xml,
		classpath: daos.xml
		</param-value>
	</context-param>
	<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
	</listener>

How to integrate multiple bean configuration xmls?

Multiple bean configuration xmls are created to separate configurations according to layers so that it becomes easy to manage and maintain them. These configuration xmls can be imported in single xml to combine all of them.

<beans>
    <import resource="services.xml"/>
    <import resource="daos.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

What are Lazily-instantiated beans?

In default behavior Spring instantiates singleton beans at the time of startup, which is called eagerly instantiation. This is good behavior as it exposes any problems in instantiation of beans at start up only. But sometimes this behavior is not expected hence by addition lazy-init=”true” to the bean definition the instantiation can be postponed to first request. Also following configuration will not allow any bean to get instantiated eagerly.

<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>

What is autowiring?
By Autowiring, Spring injects dependencies without having to specify those explicitly. Spring inspects bean factory contents and establishes relationships amongst collaborating beans. To implement it, just add autowire property in xml configuration.

What are different modes of autowiring?

Autowiring has following five modes

– no: No autowiring.

– byName: Autowiring by property name, means bean matching property name is autowired.

– byType: Bean having type as that of property type is autowired.

– constructor: Similar to byType just that the property is in constructor.

– autodetect: Spring is allowed to select autowiring from byType and constructor.

What are different bean scopes available to configure?

Following scopes can be assigned to different beans.

– singleton: One bean instance per IoC container

– prototype: Any number of instances of bean

– request: Within HTTPRequest object scope

– session: As long as HttpSession is alive

– globalsession: Within life-cycle of global HttpSession. Applicable in portlet context usually.

What is default scope in Spring?

Singleton.

How can you control bean instantiation process?

Bean instantiation by Spring can be controlled using initialization call backs. There are two ways of doing it. First is having a initialization method (say init()) and specifying it in bean configuration as ‘init-method’ property. Second is implementing InitializingBean interface and implementing afterPropertiesSet() method in it.

How can you control bean destruction process?

There are two ways of doing it. First is add a destroy() method and specify it in bean configuration as ‘destroy-method’ property. Second is implement DisposableBean interface and implement destroy() method of it.

How do you implement inheritance in bean definition?

Bean definition inheritance can be implemented by specifying ‘parent’ property of the bean equal to its parent bean definition id. This bean class must have extended itself from the parent bean class.

What are advantages of Spring usage?

– Spring provides commonly required enterprise services without a need of expensive application server.

– It reduces coupling in code and improves maintainability.

– Readily available component improve productivity and subsequently reduce development cost.

– Pojo based programming enables reuse.

– Dependency Injection can be used to improve testability.

What all you have to do to start using Spring?

– Download Spring (and dependent Jars) from Spring’s site.

– Create application context xml to define beans and dependencies.

– Integrate this xml with web.xml

– Deploy and Run the application

8 Comments

  1. Great interview interview questions.Easily understandable. Covered most of the topics. I saved this page.Thank you for your help. If you can add Hibernate integration and Transactions, that would be very helpful.

1 Trackback / Pingback

  1. JavaPins

Leave a Reply

Your email address will not be published.


*