How to get the applicationContext object of a SpringBoot project

The ApplicationContext object is the context object instance of the Spring open source framework, which automatically loads all the information in the Handler into memory when the project runs.
There are many traditional ways to obtain, but with the continuous iteration of the Spring version, the official gradually does not recommend using some methods.
Let me briefly introduce a method officially recommended by Spring!

Obtain the ApplicationContext object based on the SpringBoot platform, and manually obtain the Spring managed bean through the instance

Build project

The project in this chapter does not need too much content. Just add a Web dependency.

ApplicationContextAware

This interface object is our protagonist today. In fact, obtaining the ApplicationContext object instance by implementing the ApplicationContextAware interface is not a unique function of SpringBoot,
This interface existed as early as Spring version 3.0x, and ApplicationContext instances can also be obtained in traditional Spring projects. Let's see how to code to achieve our effect?

Implement ApplicationContextAware interface

Create an entity class and implement the ApplicationContextAware interface. Rewrite the setApplicationContext method in the interface to complete the method of obtaining the ApplicationContext instance. The code is as follows:
package com.xunmei.api;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Get Spring context object
 * */
@Component
public class ApplicationContextProvider implements ApplicationContextAware
{
    /**
     * Context object instance
     */
    private ApplicationContext applicationContext;

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

    /**
     * Get applicationContext
     * @return
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * throughGet Bean through name
     * @param name
     * @return
     */
    public Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * Get Bean. class
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * Return the specified Bean through name and Clazz
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

After we get the ApplicationContext object instance, we can manually obtain the injected instance object of the Bean,

In the ApplicationContextProvider class, I simply implemented several methods to obtain the specified Bean instance. Of course, you can add more methods to complete more business logic.

If you want to use ApplicationContext in a non Spring managed entity and do not want to inject ApplicationContextProvider to complete instantiation,

At this time, we can change the ApplicationContext instance object to a static instance and the method to a static method, so that the instance of the specified Bean can also be obtained externally. As follows:

@Component
public class ApplicationContextProvider implements ApplicationContextAware
{
    /**
     * Context object instance
     */
    private static ApplicationContext applicationContext;

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

    /**
     * Get applicationContext
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * Get Bean. By name
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * Get Bean. class
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * Return the specified Bean through name and Clazz
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}
Note here that the @ Component annotation on the ApplicationContextProvider class cannot be removed. After removal, Spring will not automatically call the setApplicationContext method to set the context instance for us.

summary

This chapter is less. It mainly explains how to use ApplicationContextAware to obtain ApplicationContext instances under SpringBoot platform, and manually obtain Bean instances managed by Spring through ApplicationContext instances
reference resources: https://www.jianshu.com/p/3cd2d4e73eb7

Posted on Thu, 28 Oct 2021 15:08:16 -0400 by Smiffy