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.

ApplicationContext Overview

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.
ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.

ApplicationContext provides the following:
  • Bean factory methods for accessing application components
  • The ability to load file resources in a generic way
  • The ability to publish events to registered listeners
  • The ability to resolve messages, supporting internationalization
ApplicationContext has several implementations. For instance, the ClassPathXmlApplicationContext takes configuration from an XML file on the classpath or AnnotationConfigApplicationContext, which reads configuration using annotations, especially @Configuration.
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!
Note 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