Spring Boot - Get ApplicationContext Examples

This tutorial shows you how to get ApplicationContext in a Spring Boot application.

In Spring, ApplicationContext is an interface that provides configuration for an application. It can be used to get registered beans, messages, and application information. Sometimes, it's necessary to access the ApplicationContext in your application code. In this tutorial, I am going to show you how to access ApplicationContext in a Spring Boot application.

Get ApplicationContext Using Dependency Injection

The easiest way to get the ApplicationContext is by using dependency injection. You need to create a bean that uses constructor injection. A simple way to create a bean is by adding @Component, @Service, @Repository, or @Controller annotation to the class. If the constructor of the bean has a parameter whose type is ApplicationContext, Spring will inject it automatically.

  package com.woolha.applicationcontext.bean;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.stereotype.Service;
  
  @Service
  public class MyService {
  
    private final ApplicationContext applicationContext;
  
    public MyService(ApplicationContext applicationContext) {
      this.applicationContext = applicationContext;
    }
  
    public void doSomething() {
      System.out.println(this.applicationContext.getId());
    }
  }

Get ApplicationContext Using ApplicationContextAware Interface

Another way to access the ApplicationContext is by creating a bean that implements ApplicationContextAware interface. Each class that implements the interface has to implement a method named setApplicationContext. When the application is started (assuming component scanning works), Spring will scan all beans that implements ApplicationContextAware and call the setApplicationContext method by passing the context as the argument. You can store it to a variable for later use.

  package com.woolha.applicationcontext.bean;
  
  import org.springframework.beans.BeansException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  import org.springframework.stereotype.Component;
  
  @Component
  public class MyComponent implements ApplicationContextAware {
  
    private ApplicationContext applicationContext;
  
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      this.applicationContext = applicationContext;
    }
  
    public void doSomething() {
      System.out.println(this.applicationContext.getId());
    }
  }
  

Get ApplicationContext in a Non-Bean Class

The above examples only work inside a Spring bean class. If you have a non-bean class and you don't want to change it into a bean, it's still possible to get the ApplicationContext. One of the solutions is by creating a bean class that stores the ApplicationContext in a static variable. For example, we can create a class that implements ApplicationContextAware. Then, store the ApplicationContext in a static variable. To make it accessible from other classes, we also need to create a static getter method.

  package com.woolha.applicationcontext.bean;
  
  import org.springframework.beans.BeansException;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  import org.springframework.stereotype.Component;
  
  @Component
  public class ApplicationContextProvider implements ApplicationContextAware {
  
    private static ApplicationContext applicationContext;
  
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      ApplicationContextProvider.applicationContext = applicationContext;
    }
  
    public static ApplicationContext getApplicationContext() {
      return applicationContext;
    }
  }

Then, you can access the static variable by calling the static getter method from any class.

  package com.woolha.applicationcontext.util;
  
  import com.woolha.applicationcontext.bean.ApplicationContextProvider;
  import org.springframework.context.ApplicationContext;
  
  public class MyUtil {
  
    private MyUtil() {
      throw new AssertionError();
    }
  
    public static void doSomething() {
      ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
      System.out.println(applicationContext.getId());
    }
  }
  

Summary

In Spring Boot, getting the ApplicationContext in a bean can be done by using dependency injection or implementing ApplicationContext aware interface. If you need to access the ApplicationContext inside a non-bean class, the workaround is to create a bean that stores the ApplicationContext as a static variable.