Spring ApplicationContext

In this tutorial we will what is spring ApplicationContext and how to access it. There are numerous different ways to get a task done in Spring framework and that is one advantage of it.

What is ApplicationContext?

ApplicationContext is an interface for providing configuration information to an application. There are multiple classes provided by springframework that implements this interface and helps us use configuration information in applications. ApplicationContext provides standard bean factory lifecycle capabilities. An important capability which we will be using in below code example is, class implementing ApplicationContext should scan for ApplicationContextAware beans and invoke setApplicationContext by passing an implementation of its instance.

ApplicationContext vs BeanFactory

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.

How to access ApplicationContext inside a java bean?

To get access to ApplicationContext we should implement ApplicationContextAware interface in the respective java bean. It has a method,

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.

We can use this utility to get application context but we need to provide the respective servletcontext as parameter. Following source code demonstrates how we can get access to application context even from inside a simple plain java bean where we don’t have access to servlet context.

ApplicationContextUtils.java

We will create the following utility class, it implements ApplicationContextAware and provides the setApplicationContext which will be invoked by spring container and the applicationContext will be passed by it. We store it in a static variable and expose it through a get method so that it can be accessed all through the application.

Download Example Source Code

Lazy-instantiation and ApplicationContext

By default spring implementations of ApplicationContext eagerly instantiate all the singleton beans at startup. This helps us to ensure all the configuration and dependencies are intact. This default behaviour can be customized as below by just adding property lazy-init=”true”.

<bean id="lazy" class="com.javapapers.LazyBean" lazy-init="true"/>

When the ApplicationContext is starting up this bean will not be instantiated and configured.

Annotation Based Access

Spring 2.5 onwards we can autowire the ApplicationContext as well as BeanFactory as below,

Very nice explanation BeanFactory actually used in case of small systems like mobile devices development , where limited resources are available like CPU cycle and memory , but application context is not sub set of BeanFactory :)

Jie,
1. You have to add the required jars. I am not able to bundle the jars, as the file size will be toooo huge.
2. You need to compile the source and generate the class file. I thought, as part of learning process you will do it :-)

>>why we need to use application context from ApplicationContextUtils in web app ?

I needed to in one case … I had a bean injected initialized to some default values in the spring application-context.xml, but in my Action class, needed to change some of the attributes (as per the values in the HttpRequest) … so retrieved the bean from AppContext, made a defensive copy and set the required values.

Awesome content. I was in urgent need of working on some modules and I was brain-fried when I saw the term Application Context in each class. I tried to make some sense and understand it from the name but it was not very helpful.
Your article was very helpful. It helped me understand without going into the details of its methods or hierarchies.
Thanks.
Although, a suggestion:
Sometimes a pictorial representation or a side link or a downloadable file to the hierarchies of a class, method and constructor lists etc will be very helpful.
That will make your posts a one stop solution.