Home » Spring » Spring IOC Container

Spring IOC Container

Java Application involve writing a large number of classes.Associating these classes with each other is complicated task.Re usability of a specific class becomes an issue with traditional approach as it is coupled with other classes.Reusing of classes can be achieved only if they are loosely coupled with each other.Spring framework takes care of wiring the classes together due to which classes becomes loosely coupled. Spring framework provides a lightweight container that injects the required functionalities into the code.

In a typical Spring based application, we need to create Simple Java Beans and wire them and let the container manage them. The container gets all the instruction to create, configure and manage the beans from the bean configuration metadata. The configurations can be done through Java code and/or annotations and/or XML files.

Spring provides two interfaces that acts as containers, namely BeanFactory and ApplicationContext .A BeanFactory just initiates and configures beans. An ApplicationContext also does that, and also provides more supporting infrastructure that enables lots of enterprise specific features such as Aspect Oriented Programming and transaction. Thus, it is better to use ApplicationContext rather than BeanFactory , in most cases.Let's discuss BeanFactory and ApplicationContext containers in detail.

BeanFactory

The org.springframework.beans.BeanFactory container is the root container that loads all the beans and provides dependency injection to enterprise applications. XmlBeanFactory and StaticListableBeanFactory are some of the classes that implements BeanFactory interface.

One of the most popularly used implementation of BeanFactory is the XMLBeanFactory . XMLBeanFactory allows the representation of objects and their rich dependencies in terms of XML. The XmlBeanFactory takes XML configuration metadata to create the fully configured application.

To use the BeanFactory, we need to create the instance of XmlBeanFactory class as given below:

Resource resource=new ClassPathResource("applicationContext.xml");  
BeanFactory factory=new XmlBeanFactory(resource);  

The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory.

The following sample shows a simple hello world application using the XmlBeanFactory .

HelloWorld.java is the Bean class whose code is given below.

HelloWorld.java

package com.jwt.spring;

public class HelloWorld {
	private String message;

	public void setMessage(String message) {
		this.message = message;
	}

	public void getMessage() {
		System.out.println("Here is the output : " + message);
	}
}

The below XML code shows the contents of the bean xml configuration. It has a single bean configured that has a single property by the name message. A default value is set for the property.

Beans.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
<bean id="helloWorld" class="com.jwt.spring.HelloWorld"> 
<property name="message" value="Hello World!"/> 
</bean>

below is the code for the Test.java that uses the XMLBeanFactory to create the HelloWorld bean and invoke a method in the created Spring bean.

Test.java

package com.jwt.spring;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Test {
	public static void main(String[] args) {
		XmlBeanFactory factory = new XmlBeanFactory(
				newClassPathResource("Beans.xml"));
		HelloWorld helloObj = (HelloWorld) factory.getBean("helloWorld");
		helloObj.getMessage();
	}
}

In the above code, the XmlBeanFactory loads the metadata based on the CLASSPATH variable. It uses the configuration metadata to load, assemble and dispense the beans upon request. Finally, a method on the bean (getMessage()) is invoked to get the desired result.

The BeanFactory is preferred when there are limited resources like mobile devices or applets. BeanFactory is a subset of ApplicaitonContext and provides lesser functionalities. When we need full capabilities with respect to configuration handling then we go for ApplicationContext.

ApplicationContext

The org.springframework.context.ApplicationContext Container is the widely used container in the enterprise applications.The ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory. BeanFactory can still be used for lightweight applications like mobile devices or applet based applications where data volume and speed is significant.

It is an advance container that extends the BeanFactory container with various enterprise-level features, given as follows:

  • Ability to resolve textual messages from a properties file and helps in internationalizing the applications.
  • Integrates with Spring Aspect Oriented Programming(AOP) to generate proxy classes.
  • Publishes events to all registered subscribers.

The most popularly used ApplicationContext implementations are:

  • FileSystemXmlApplicationContext - This implementation loads the definitions of the beans from an XML file. It is required to provide the full path of the XML bean configuration file to the constructor.
  • ClassPathXmlApplicationContext This container loads the definitions of the beans from an XML file. However, it is not required to provide the full path of the XML file. It does require you to set CLASSPATH properly because this container will look for bean configuration XML file in the specified CLASSPATH.
  • XmlWebApplicationContext : This container loads the XML file with definitions of all beans from within a web application.

The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface. We need to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext as given below:

ApplicationContext context =   
    new ClassPathXmlApplicationContext("applicationContext.xml");  

The constructor of ClassPathXmlApplicationContext class receives string, so we can pass the name of the xml file to create the instance of ApplicationContext.


Previous Next Article
comments powered by Disqus