ApplicationContext Implementation Classes

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

ApplicationContext Interface Implementation Classes

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

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

    // servlet configuration

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course