What is ApplicationContext Interface and It's Implementation Classes

In this quick article, we will take a look into what is ApplicationContext interface, and its implementation classes with an example.

Learn Spring core concepts at Spring Core Tutorial

What is ApplicationContext Interface?

The ApplicationContext is the central interface within a Spring application for providing configuration information to the application. 

The interfaces BeanFactory and ApplicationContext represent the Spring IoC container. Here, BeanFactory is the root interface for accessing the Spring container. It provides basic functionalities for managing beans.

On the other hand, the ApplicationContext is a sub-interface of the BeanFactory. Therefore, it offers all the functionalities of BeanFactory. Furthermore, it provides more enterprise-specific functionalities. 

The important features of ApplicationContext are 
  • resolving messages
  • supporting internationalization,
  • publishing events
  • application-layer-specific contexts
This is why we use ApplicationContext as the default Spring container.

ApplicationContext Interface Implementation Classes

The below diagram shows the implementations of BeanFactory and ApplicationContext interfaces:

Let's take a look into all the ApplicationContext Interface implementation classes.

1. FileSystemXMLApplicationContext

We use the FileSystemXMLApplicationContext class to load an XML-based Spring configuration file from the file system or from URLs. 

For example, let's see how we can create this Spring container and load the beans for our XML-based configuration:

String path = "C:/Spring-demo/src/main/resources/spring-servlet.xml";

ApplicationContext appContext = new FileSystemXmlApplicationContext(path);
AccountService accountService = appContext.getBean("studentService", StudentService.class);

2. ClassPathXmlApplicationContext

In case we want to load an XML configuration file from the classpath, we can use the ClassPathXmlApplicationContext class. 

Similar to FileSystemXMLApplicationContext, it's useful for test harnesses, as well as application contexts embedded within JARs.

For example:
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-servlet.xml");
AccountService accountService = appContext.getBean("studentService", StudentService.class);

3. XmlWebApplicationContext

If we use the XML-based configuration in a web application, we can use the XmlWebApplicationContext class.

Configuration classes declared and typically loaded from XML file in /WEB-INF/

For example:
public class MyXmlWebApplicationInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocation("/WEB-INF/spring/applicationContext.xml");
    context.setServletContext(container);

    // Servlet configuration
  }
}

4. AnnotationConfigApplicationContext

The AnnotationConfigApplicationContext class was introduced in Spring 3.0. It can take classes annotated with @Configuration, @Component, and JSR-330 metadata as input.

So let's see a simple example of using the AnnotationConfigApplicationContext container with our Java-based configuration:
ApplicationContext appContext = new AnnotationConfigApplicationContext(StudentConfig.class);
AccountService accountService = appContext.getBean(StudentService.class);

5. AnnotationConfigWebApplicationContext

The AnnotationConfigWebApplicationContext is a web-based variant of AnnotationConfigApplicationContext.

We may use this class when we configure Spring's ContextLoaderListener servlet listener or a Spring MVC DispatcherServlet is in a web.xml file.

Moreover, from Spring 3.0 onward, we can also configure this application context container programmatically. All we need to do is implement the WebApplicationInitializer interface:

public class MyWebApplicationInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(StudentConfig.class);
    context.setServletContext(container);

    // servlet configuration
  }
}

Conclusion

In this quick article, we have seen what is ApplicationContext interface, and its implementation classes briefly with an example.

What's next

Learn Spring core important concepts at Spring Core Tutorial

Comments