ApplicationContext Interface and Its Implementation Classes

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.

Features of ApplicationContext

Here are some key features provided by the ApplicationContext:

  1. Bean Instantiation/Wiring: Automatically handles the instantiation and wiring of beans.
  2. Automatic BeanPostProcessor Registration: Registers BeanPostProcessor implementations, allowing custom modification of new bean instances.
  3. Automatic BeanFactoryPostProcessor Registration: Registers BeanFactoryPostProcessor implementations, allowing custom modification of the application context's internal bean factory.
  4. Convenient MessageSource Access: Provides access to message sources, allowing easy internationalization.
  5. Application Event Publication: Supports event propagation, allowing beans to publish and listen to application events.
  6. Resource Loading: Provides a generic way to load file resources, such as classpath, file system, and URLs.

ApplicationContext Interface

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
}

Common ApplicationContext Implementations

Spring provides several implementations of the ApplicationContext interface, each serving different use cases:

1. AnnotationConfigApplicationContext

AnnotationConfigApplicationContext is used for standalone Java applications that use annotations for configuration.

Example:

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();
    }
}

2. ClassPathXmlApplicationContext

ClassPathXmlApplicationContext loads the bean configuration XML file from the classpath.

Example:

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();
    }
}

3. FileSystemXmlApplicationContext

FileSystemXmlApplicationContext loads the bean configuration XML file from anywhere in the file system.

Example:

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();
    }
}

4. AnnotationConfigWebApplicationContext and XmlWebApplicationContext

These are used for web applications, providing context configuration for Spring MVC applications using annotations and XML, respectively.

Example for AnnotationConfigWebApplicationContext:

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));
    }
}

Example for XmlWebApplicationContext:

<!-- web.xml -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

Conclusion

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.

Comments