ApplicationContext Interface

The ApplicationContext is the central interface within a Spring application for providing configuration information to the application. It is read-only at run time, but can be reloaded if necessary and supported by the application. A number of classes implement the ApplicationContext interface, allowing for a variety of configuration options and types of applications.

The ApplicationContext provides:

  • Bean factory methods for accessing application components.

  • The ability to load file resources in a generic fashion.

  • The ability to publish events to registered listeners.

  • The ability to resolve messages to support internationalization.

  • Inheritance from a parent context.

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

Read more details here.


Configuring ApplicationContext 

You can configure a Spring application through xml, or Java configuration or annotations along with xml or java configuration. 

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

Note: 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");



When using the ApplicationContext type, you may see a warning that: Resource leak: 'context' is never closed.  This is because you don’t have a close method with BeanFactory or even ApplicationContext.

AbstractApplicationContext is an abstract implementation of the ApplicationContext interface and it also implements other 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.



By default, your bean will not be aware about the ApplicationContext. If you need your bean to have access to the application context (usually not required), 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.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)