Saturday, August 25, 2012

Spring ApplicationContext

This is 10 of 13 parts of tutorial series

Tutorial Content:


Part-1:Introduction to spring framework
Part-2:Dependency injection(ioc) in spring
Part-3:Spring hello world example in eclipse
Part-4:Dependency injection via setter method in spring
Part-5:Dependency injection via constructor in spring
Part-6:Spring Bean scopes with examples
Part-7:Initializing collections in spring
Part-8:Beans Autowiring in spring
Part-9:Inheritance in Spring
Part-10:Spring ApplicationContext
Part-11:Spring lifetime callbacks
Part-12:BeanPostProcessors in Spring
Part-13:Annotation based Configuration in spring

In this tutorial we will learn about what is ApplicationContext and how we can access it.

ApplicationContext:

ApplicationContext is an central interface for providing configuration information to an application.

An ApplicationContext provides the following functionalities:
  • Bean factory methods, inherited from ListableBeanFactory. This avoids the need for applications to use singletons.
  • The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
  • The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
  • The ability to publish events. Implementations must provide a means of registering event listeners.
  • Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.

 ApplicationContext vs BeanFactory:

We can get our bean from both by applying getBean method.BeanFactory is subset of ApplicationContext and provides less functionalities.So if we want to use full functionalities then we go for ApplicationContext.

Getting ApplicationContext in Bean Class:

To get access to ApplicationContext we should implement ApplicationContextAware interface in the respective java bean. 

ApplicationContextAware  Interface:

 It has a method,
public void setApplicationContext(ApplicationContext applicationContext)

            throws BeansException
The ApplicationContext implementation which we are using in our application will invoke this method and pass the concrete object for AppplicationContext. Using this we can get access to all the configuration information.

Implementing ApplicationContextAware interface makes sense for example when an object requires access to a set of collaborating beans. Note that configuration via bean references is preferable to implementing this interface just for bean lookup purposes.

This interface can also be implemented if an object needs access to file resources, i.e. wants to call getResource, wants to publish an application event, or requires access to the MessageSource. However, it is preferable to implement the more specific ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware interface in such a specific scenario.

Example:

For configuring spring in your eclipse ide please refer  hello world example

1.Country.java:

This is simple pojo class having some attributes so here country has name and object of Capital class.
Create Country.java under package org.arpit.javapostsforlearning.Copy following content into Country.java.

package org.arpit.javapostsforlearning;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Country implements ApplicationContextAware{

String countryName ;
ApplicationContext applicationContext;
Capital capital;
public String getCountryName() {
return countryName;
}


public void setCountryName(String countryName) {
this.countryName = countryName;
}


public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext=applicationContext;
}

public String getCapitalName(String capitalBeanName)
{
capital=(Capital) applicationContext.getBean(capitalBeanName);
String capitalName=capital.getCapitalName();
return capitalName;
}
}

2.Capital.java

This is also simple pojo class having one attribute called "capitalName".
Create Capital.java under package org.arpit.javapostsforlearning.java.Above Country class contains object of this class.Copy following content into Capital.java

package org.arpit.javapostsforlearning;

public class Capital {

    String capitalName;

    public String getCapitalName() {
        return capitalName;
    }

    public void setCapitalName(String capitalName) {
        this.capitalName = capitalName;
    }
}

3.ApplicationContextMain.java

This class contains main function.Create ApplicationContextMain.java under package org.arpit.javapostsforlearning.Copy following content into ApplicationContextMain.java
 package org.arpit.javapostsforlearning;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextMain{

public static void main(String[] args) {

ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Country countryObj = (Country) appContext.getBean("country");
System.out.println("Capital Name:"+countryObj.getCapitalName("capital"));
}
}
You can note here that we have used ClassPathXmlApplicationContext for getting bean here.There are various ways for getting beans.In hello world example we have used XmlBeanFactory for getting beans.

4.ApplicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="country" class="org.arpit.javapostsforlearning.Country" >
<property name="countryName" value="India"/>
</bean>

<bean id="capital" class="org.arpit.javapostsforlearning.Capital" >
<property name="capitalName" value="Delhi"/>
</bean>
</beans>
As you can see, there is no connection between above two beans in this XML file.We are getting object of country class with help of getBean method and then passing id of  capital class to getCapitalName method of country class.In getCapital method,we have ApplicationContext object which is initialized by setApplicationContext method by spring container ,so with help of ApplicationContext object,we are calling getBean method for initializing capital object and getting capitalName from that object.

5.Run it

When you will run above application,you will get following as output.
India's Capital Name:Delhi

Source code:


In next post,we will see spring lifetime callbacks.

 



No comments:

Post a Comment