In depth understanding of the use of ApplicationContextAware

When review ing the code of predecessors, I saw a tool class. SpringContextUtil didn't quite understand its usage

@Component
public class SpringContextUtil implements ApplicationContextAware {

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

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static <T> T getBean(String name,Class<T> clazz) {
        return applicationContext.getBean(name,clazz);
    }
}

Then I found some blog s to summarize here.

We get the bean s in the container, usually in this way:

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-common.xml");  
AbcService abcService = (AbcService)appContext.getBean("abcService");  

But there will be a problem: because it will reload ApplicationContext common XML and instantiate the context bean. If some thread configuration classes are also in this configuration file, the thread doing the same work will be started twice. One is started when the web container is initialized, and the other is instantiated as shown in the above code. When re initializing!!!! This creates redundancy. It is a terrible thing in project development.

Spring provides a way to solve this problem: get the instantiated bean from the existing spring context. It is implemented through the ApplicationContextAware interface. When a class implements this interface (ApplicationContextAware), this class can easily obtain all beans in ApplicationContext. In other words, this class can directly obtain all referenced bean objects in the spring configuration file.

How does ApplicationContextAware work?

@Component
public class AppUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        applicationContext = arg0;
    }

    public static Object getObject(String id) {
        Object object = null;
        object = applicationContext.getBean(id);
        return object;
    }
}

In the spring configuration file, register the method class AppUtil. The reason why the method class AppUtil can flexibly obtain ApplicationContext is that spring can automatically execute setApplicationContext for us. However, spring will not execute its methods for a class for no reason. Therefore, it is necessary to inform spring of the existence of such a class by registering the method class AppUtil. Here we use @ Component to register.

When loading the Spring configuration file, if the Bean class defined in the Spring configuration file implements the ApplicationContextAware interface, the Bean class in the ApplicationContextAware interface will be automatically called when loading the Spring configuration file

public void setApplicationContext(ApplicationContext context) throws BeansException

Get the ApplicationContext object, which is injected by spring. This class must be specified in the spring configuration file.

Keywords: Spring application Context

Added by SauloA on Fri, 11 Feb 2022 10:28:18 +0200