MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Spring IoC Container in Java

This article will discuss about the Spring IoC Container in Java.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Spring IoC Container

The IoC container forms the core module of spring’s architecture. The container is in complete control of a bean’s life cycle. It creates, configures and manages every bean configured in the configuration metadata. The beans managed by the container all called Spring Beans.

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. The following diagram is a simple representation of the Spring container process towards creating a fully configured application.

The Spring IoC Container

Figure 1: The Spring IoC Container

The Spring container provides the IoC principle through two packages - org.springframework.beans and org.springframework.context. The BeanFactory and ApplicationContext interface are the two available options. 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. In short, the BeanFactory provides the configuration framework and basic functionality, while theApplicationContext adds more enterprise-centric functionality to it. The ApplicationContext is a complete superset of the BeanFactory, and any description of BeanFactory capabilities and behavior is to be considered to apply to the ApplicationContext as well.

Spring Beans

A bean is a simple java object that is instantiated, configured, and managed by a Spring IoC container. Other than being managed by Spring IoC container, there is nothing special about these beans. In all other respects, these beans are just regular object in an application. These beans and their dependencies between each other are represented in the configuration metadata used by the Spring container. It is a called a bean instead of being called an object or component because of the fact the origins of the Framework - a replacement to complex and heavy Enterprise JavaBeans.

BeanFactory

org.springframework.beans.BeanFactory is the actual representation of the Spring IoC container responsible for containing and managing the Spring beans. The BeanFactory, sources application object, configures them and takes care of assembling the dependencies between objects. 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

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

Listing 1: XmlBeanFactory

.	
<?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.src.sample.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>

The above 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.

Next is the HelloWorld.java code that is configured in the configuration file..

Listing 2: HelloWorld.java

packagecom.src.sample; 
public class HelloWorld { 
private String message; 
public void setMessage(String message){ 
this.message = message; 
} 
public void getMessage(){ 
System.out.println("Printing the message : " + message); 
} 
}

Finally, here is the code for the HelloWorldMain.java that uses the XMLBeanFactory to create the HelloWorld bean and invoke a method in the created Spring bean.

Listing 3: Spring bean creation

packagecom.src.sample; 
importorg.springframework.beans.factory.InitializingBean; 
importorg.springframework.beans.factory.xml.XmlBeanFactory; 
importorg.springframework.core.io.ClassPathResource; 
public class HelloWorldMain {
public static void main(String[] args) { 
XmlBeanFactory factory = new XmlBeanFactory
(newClassPathResource("Beans.xml")); 
HelloWorldobj = (HelloWorld) factory.getBean("helloWorld"); 
obj.getMessage(); 
} }

In the above code, the XmlBeanFactory loads the metadata XML 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. It is better to use the ApplicationContext unless there is a very good reason not to.

ApplicationContext

ApplicationContext is defined by the org.springframework.context.ApplicationContext interface. This is similar to BeanFactory, in that it can load bean definitions, wire them together and dispense beans upon request. In addition, ApplicationContext can also perform more enterprise functionalities like transaction, AOP, resolving text messages from properties files, and push application events to interested listeners.

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 following code shows how to use the FileSystemXmlApplicationContext.

Listing 4: FileSystemXmlApplicationContext

public static void main(String[] args) {
	ApplicationContext context = new FileSystemXmlApplicationContext
	("C:/MyWorkspace/workspace/HelloSpring/src/Beans.xml"); 
	HelloWorldobj = (HelloWorld) context.getBean("helloWorld");
	obj.getMessage(); 
}

In FileSystemXmlApplicationContext, the complete file path is given. The xml under the file path is loaded by the container and later dispensed upon request.

The following sample shows the code for creating a Hello World application using ClassPathXmlApplicationContext.

Listing 5: HelloWorld.java

packagecom.src.sample;
public class HelloWorld { 
private String message; 
public void setMessage(String message){ 
this.message = message; 
} 
public void getMessage(){ 
System.out.println("Printing the message : " + message); 
} 
}

Listing 6: HelloWorldMain.java

packagecom.src.sample
importorg.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class HelloWorldMain{
public static void main(String[] args) {
ApplicationContext context = 
newClassPathXmlApplicationContext("Beans.xml"); 
HelloWorldobj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage(); 
}

As shown above, while using the ClassPathXmlApplicationContext, it is not required to provide the complete Xml file location. The implementation uses the CLASSPATH variable to determine the file location. Here, the first step is to create a factory object where the framework APIClassPathXmlApplicationContext is used to create the factory bean after loading the bean configuration file from the CLASSPATH. The ClassPathXmlApplicationContext() API takes care of creating and initializing all the objects (ie. Spring beans mentioned in the XML bean configuration file). Second step is used to get the required bean using getBean() method of the created context. This method uses bean ID to return a generic object which finally can be casted to the actual HelloWorldobject. The required bean can be used to call any class method. Here we call the getMessage method that prints the required message.

Finally, here is the content of the bean configuration xml

Listing 7: XML Configuration

<?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.src.sample.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>

The above configured file has only one bean configuration that has a simple property by name “message”. A default value for the message property can be set during the configuration.

This is all for today’s article. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login