Engineering Full Stack Apps with Java and JavaScript
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.
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.
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;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JJWriterMain {
public static void main(String[] args) {
AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
JJWriter writer = (JJWriter)context.getBean("jjwriter");
writer.write();
context.close();
}
}
If you execute this new class, you can see below INFO messages in the console after printing the print message:
org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext…
org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons…
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory…
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.