Spring ApplicationContext

This is 12 of 16 parts of tutorial series

Tutorial Content: Spring tutorial for beginners

    Introduction to spring framework Spring interview questions Dependency injection(ioc) in spring Spring hello world example in eclipse Spring java based configuaration Dependency injection via setter method in spring Dependency injection via constructor in spring Spring Bean scopes with examples Initializing collections in spring Beans Autowiring in spring Inheritance in Spring Spring ApplicationContext Spring lifetime callbacks BeanPostProcessors in Spring Annotation based Configuration in spring Spring AOP tutorial
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 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 {
 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 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;


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.


<?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 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.


Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012