Sunday, 26 August 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 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.


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

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

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 {
 public String getCapitalName(String capitalBeanName)
  capital=(Capital) applicationContext.getBean(capitalBeanName);
  String capitalName=capital.getCapitalName();
  return capitalName;

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

package org.arpit.javapostsforlearning;

public class Capital {

    String capitalName;

    public String getCapitalName() {
        return capitalName;

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

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

import org.springframework.context.ApplicationContext;

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.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:xsi="" xmlns:aop=""

 <bean id="country" class="org.arpit.javapostsforlearning.Country" >
 <property name="countryName" value="India"/>
 <bean id="capital" class="org.arpit.javapostsforlearning.Capital" >
 <property name="capitalName" value="Delhi"/>
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.



Java tutorial for beginners Copyright © 2012