Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW