View RSS Feed

Spring Framework

The Core Features of the Spring Framework

Rate this Entry
by , 11-15-2011 at 02:42 PM (2109 Views)
Dependency Injection
In early tips, we discussed the issues around creating and initializing objects and it’s variables. Fortunately there is a way of addressing this situation in Spring that is a wonderful design pattern that they have applied to address this aspect. It is a combination of Inversion of Control and Dependency Injection. Many developers mistakenly believe that IOC and DI are the same thing. It is incorrect. IOC deals with inverting the control flow in an application, DI describes how one object resolves or finds other objects on which it needs to invoke some methods. There are a number of ways to achieve DI, and one such strategy is IOC. There are two types of DI, one is Dependency Pull (objects are pulled from the registry) and the other is Dependency Push (objects are pushed from the registry). Below I will show how this is accomplished via Spring Framework’s bean factory.

Bean Factory
The org.springframework.beans.factory.BeanFactory interface provides the basis for Spring’s IOC container or bean factory. It is an implementation of the GOF factory method design pattern and it creates, caches, wires together, and manages application objects. These objects are called beans, similar to Java Beans but using plain old java objects (POJOs). This is the basis for Spring programming model. Spring provides a number of out-of-the-box implementations for the bean factory. One implementation is the XmlBeanFactory class. The XmlBeanFactory class allows you to configure the various application classes and their dependencies in XML files. In short, a bean factory is like a registry for application objects. The listing below shows a simple Spring bean configuration file.

Java Code:
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns=""
<bean name="carService" class="com.acme.example.service.CatServiceImpl" />
Now that the bean has been wired in the XML configuration file, you can start the IOC container.

Java Code: Resource res = new FileSystemResource("spring-config.xml");
BeanFactory factory = new XmlBeanFactory(res);
Now that the Spring container is running, you can retrieve beans from the bean factory to perform useful work in the application.

This is a straightforward process as is shown in the example below. The application code uses a Spring bean factory or IOC container to retrieve the car service objects using the specified key. Now it is possible to have several variants of the CarSevice depending on the type of the car. This is because each car is different and provides a different set of features and options. It would be cumbersome to invoke the getBean method each time you need a bean. The way that Spring Framework addresses this is to implement a factory method using Dependency Injection for any Car object.

Java Code: CarService service = 
	(CarService) factory.getBean("carService");
One major goal of Spring is to be unobtrusive and impose minimal dependency on the framework. This is achieved through different forms of Dependency Injection supported by the Spring IOC container.

Submit "The Core Features of the Spring Framework" to Facebook Submit "The Core Features of the Spring Framework" to Digg Submit "The Core Features of the Spring Framework" to Submit "The Core Features of the Spring Framework" to StumbleUpon Submit "The Core Features of the Spring Framework" to Google