Partner – Microsoft – NPI (cat= Spring)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

1. Overview

In this short tutorial, we’ll see how to get the current ApplicationContext in a Spring application.

2. ApplicationContext

ApplicationContext represents the Spring IoC container that holds all the beans created by the application. It is responsible for instantiating, configuring, and creating the beans. Additionally, it gets the beans’ information from configuration metadata provided in XML or Java.

ApplicationContext represents the sub-interface of BeanFactory. In addition to the functionalities of BeanFactory, it includes features like message resolving and internationalization, resource loading, and event publishing. Furthermore, it has the functionality of loading multiple contexts.

Each bean is instantiated after the container has been started since it uses eager loading.

We may want to use this container to access other beans and resources in our application. We’ll learn two ways to get the current ApplicationContext reference in the Spring application.

3. ApplicationContext Bean

The simplest way to get the current ApplicationContext is by injecting it into our beans using the @Autowired annotation.

Firstly, let’s declare the instance variable and annotate it with the @Autowired annotation:

@Component
public class MyBean {

    @Autowired
    private ApplicationContext applicationContext;

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}

Instead of @Autowired, we could use the @Inject annotation.

To verify the container was properly injected, let’s create a test:

@Test
void whenGetApplicationContext_thenReturnApplicationContext(){
    assertNotNull(myBean);
    ApplicationContext context = myBean.getApplicationContext();
    assertNotNull(context);
}

4. ApplicationContextAware Interface

Another way to get the current context is by implementing the ApplicationContextAware interface. It contains the setApplicationContext() method, which Spring calls after creating ApplicationContext.

Furthermore, when an application starts, Spring automatically detects this interface and injects a reference to ApplicationContext.

Now, let’s create the ApplicationContextProvider class that implements the ApplicationContextAware interface:

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

We declared the applicationContext instance variable as static so we can access it in any class. Additionally, we created a static method for retrieving the reference to ApplicationContext.

Now, we can get the current ApplicationContext object by calling the static getApplicationContext() method:

@Test
void whenGetApplicationContext_thenReturnApplicationContext() {
    ApplicationContext context = ApplicationContextProvider.getApplicationContext();
    assertNotNull(context);
}

Furthermore, by implementing the interface, a bean can obtain a reference to ApplicationContext and access other beans or resources.

To accomplish this, firstly, let’s create the ItemService class:

@Service
public class ItemService {
    // ...
}

Secondly, to get the ItemService bean from the context, let’s call the getBean() method on ApplicationContext:

@Test
void whenGetBean_thenReturnItemServiceReference() {
    ApplicationContext context = ApplicationContextProvider.getApplicationContext();
    assertNotNull(context);

    ItemService itemService = context.getBean(ItemService.class);
    assertNotNull(context);
}

5. Conclusion

In this short article, we learned how to get the current ApplicationContext in our Spring Boot application. To summarize, we could inject the ApplicationContext bean directly or implement the ApplicationContextAware interface.

As always, the entire source code can be found on GitHub.

Course – LS (cat=Spring)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.