ApplicationContext Interface and Its Hierarchy

The BeanFactory provides configuration mechanism capable of managing beans (objects), using different kinds of storage facility. The getBean() method is part of the org.springframework.beans.factory.BeanFactory.

The ApplicationContext extends from the BeanFactory interface, adding additional functionality. such as easier integration with Springs AOP features, event propagation, declarative mechanisms to create the ApplicationContext and optional parent contexts, and application-layer specific contexts such as the WebApplicationContext. 

In addition to standard BeanFactory lifecycle capabilities, ApplicationContext implementations detect and invoke ApplicationContextAware beans as well as ResourceLoaderAware, ApplicationEventPublisherAware and MessageSourceAware beans. ApplicationContext also involves applying AOP to the beans.


Configuring ApplicationContext 

You can configure a Spring application through xml, or Java configuration or annotations along with xml or java configuration. There are examples for all these approaches within this section.

Configuration classes can be abstract, but not final, as Spring will create a dynamic subclass of the configuration class for parsing.

The configuration class will not be automatically picked up by spring; we need to create the context along with this class. There are multiple ways to do this. For instance, in a servlet based web application this might be done from WebApplicationInitializer. WebApplicationInitializer will be called by Spring's SpringServletContainerInitializer as part of the JEE application startup to configure our JEE and Spring configuration. 


Resource Loading

Different ApplicationContext implementations have different default resource loading mechanisms like ClassPathXmlApplicationContext's default is classpath and FileSystemXmlApplicationContext's default is filesystem. 

ApplicationContext context = new FileSystemXmlApplicationContext( "classpath:/com/javajee/spring/annotation/application-context.xml");



If you are in eclipse and using an ApplicationContext reference type, and if you hover over the context reference variable in the declaration line, you will see a warning that: Resource leak: 'context' is never closed.  However you don’t have a close method with BeanFactory or even ApplicationContext.

AbstractApplicationContext is an abstract implementation of the ApplicationContext interface and it implements many more interfaces like Closeable and AutoCloseable. It has a close method that closes this application context, destroying all beans in its bean factory.

ClassPathXmlApplicationContext indirectly extends AbstractApplicationContext. Therefore we can use AbstractApplicationContext instead of ApplicationContext and call the close() method:

package com.javajee.spring;



public class JJWriterMain {

  public static void main(String[] args) {

    AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");

    JJWriter writer = (JJWriter)context.getBean("jjwriter");





If you execute this new class, you can see below INFO messages in the console after printing the print message: doClose

INFO: Closing… destroySingletons…

INFO: Destroying singletons in…

You can also register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time. You do this by calling the registerShutdownHook() method.

Similarly there are other callback mechanisms which can be configured by implementing interfaces (e.g. InitializingBean and DisposableBean) and also by configuring in the spring xml (e.g. init-method and destroy-method attributes of the bean element, and default-init-method and default-destroy-method of the beans element which is applicable to all bean elements within it).



By default, a bean itself will not be aware about the ApplicationContext. In certain cases you might want access to the application context from your bean, even though that is not a good practice as you are creating dependency with Spring. However, if you need your bean to have access to the application context, your bean needs to implement an interface known as ApplicationContextAware and use the methods provided by that interface. 

There are also other similar interfaces (e.g. BeanNameAware) that provide us access to spring configuration details from our bean, but it is always adviced not to use these unless it is really necessary.



There are many more derivations of ApplicationContext that may suite your special need. The org.springframework.web.context.WebApplicationContext interface is a specialized version of the ApplicationContext interface used in web environments. Please refer to spring documentation for more details.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!