How to Get Application Context in Spring Boot

In this article, I show you how to get the ApplicationContext object in Spring boot application and how to get the bean from ApplicationContext.
The ApplicationContext is the central interface within a Spring application for providing configuration information to the application. The ApplicationContext interface provides the getBean() method to retrieve bean from the spring container.
Its main function is to support the creation of big business applications. Here are the features it provides:
  • Bean instantiation/wiring
  • Automatic BeanPostProcessor registration
  • Automatic BeanFactoryPostProcessor registration
  • Convenient MessageSource access (for i18n)
  • ApplicationEvent publication
The ApplicationContext interface uses eager loading, so every bean instantiate after the ApplicationContext started up.

How to Get ApplicationContext in Spring Boot

I show you two ways to get the ApplicationContext object in Spring boot application and how to get the bean from ApplicationContext.

1. Implementing ApplicationContextAware Interface

Let's create a simple Spring component:
package net.javaguides.spring;

import org.springframework.stereotype.Component;

@Component
public class Message {

    public String getMessage() {
        return "Hello World!";
    }
}
Note that Message class annotated with @Component annotation. 
Let's create ApplicationContextProvider class which implement ApplicationContextAware as in the following example:
package net.javaguides.spring;

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 ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }
}
Note that the ApplicationContextProvider class annotated with @Component annotation.
Below snippet shows how to access ApplicationContext object and invoke it's getBean() method to obtain bean in spring boot main class:
package net.javaguides.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringGetApplicationContextApplication implements CommandLineRunner {

    @Autowired
    ApplicationContextProvider applicationContextProvider;

    public static void main(String[] args) {
        SpringApplication.run(SpringGetApplicationContextApplication.class, args);
    }

    @Override
    public void run(String...args) throws Exception {

        Message message = applicationContextProvider.getApplicationContext().getBean(Message.class);
        System.out.println(message.getMessage());
    }
}
Output:
Hello World!
Note that you can access the ApplicationContext object anywhere in your spring boot application.

Field injection ApplicationContext (@Autowired and Field injection)

You can Autowire the ApplicationContext as a field:
    @Autowired
    private ApplicationContext applicationContext;
Here is the complete code:
package net.javaguides.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringGetApplicationContextApplication implements CommandLineRunner {

    @Autowired
    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(SpringGetApplicationContextApplication.class, args);
    }

    @Override
    public void run(String...args) throws Exception {

        Message message = applicationContext.getBean(Message.class);
        System.out.println(message.getMessage());
    }
}
Output:
Hello World!
Nore that field injection technique is best avoided due to issues that may arise.

Conclusion

That's it. We have looked at how to get the ApplicationContext object in Spring boot application and how to get the bean from ApplicationContext.

Comments