The ApplicationContext
is a central interface to provide configuration for a Spring application.
It is part
of the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The
ApplicationContext
interface extends the BeanFactory interface, adding more enterprise-specific
functionalities.
Here are some key features provided by the ApplicationContext:
The ApplicationContext
interface is defined as follows:
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
// Various methods to provide configuration, resource loading, and bean management
}
Spring provides several implementations of the ApplicationContext
interface, each serving
different use cases:
AnnotationConfigApplicationContext
is used for standalone Java applications that use annotations
for configuration.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();
}
}
ClassPathXmlApplicationContext
loads the bean configuration XML file from the classpath.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();
}
}
FileSystemXmlApplicationContext
loads the bean configuration XML file from anywhere in the file
system.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("C:/path/to/applicationContext.xml");
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();
}
}
These are used for web applications, providing context configuration for Spring MVC applications using annotations and XML, respectively.
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebConfig.class);
servletContext.addListener(new ContextLoaderListener(context));
}
}
<!-- web.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener>
<context-param>
<param-name>contextConfigLocation
<param-value>/WEB-INF/applicationContext.xml
</context-param>
The ApplicationContext
interface is a powerful and flexible part of the Spring IoC container. It
extends the
basic functionalities of the BeanFactory
and provides advanced features such as
internationalization, event
propagation, and resource management. Understanding the different implementations of
ApplicationContext
and
their use cases is crucial for effectively managing and configuring your Spring applications.