首頁  >  文章  >  Java  >  SpringBoot ApplicationContextAware拓展介面如何使用

SpringBoot ApplicationContextAware拓展介面如何使用

王林
王林轉載
2023-05-15 17:04:06914瀏覽

ApplicationContextAware介面:

public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

首先Aware介面就知道這是springboot擴充給使用者使用的,這裡提供了方法setApplicationContext,參數就是傳遞spring容器上下文物件進來,我們可以接收這個上下文對象,我們要知道取得spring容器上下文ApplicationContext具體有什麼作用,這才是擴充介面的目的所在,取得上下文會根據上下文的特性做些什麼。

我們來看ApplicationContext物件的方法:

SpringBoot ApplicationContextAware拓展介面如何使用

#來看看AbstractApplicationContext實作類別的方法:

    public Object getBean(String name) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(name);}
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(name, requiredType);}
    public Object getBean(String name, Object... args) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(name, args);}
    public <T> T getBean(Class<T> requiredType) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(requiredType);}
    public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {this.assertBeanFactoryActive();return this.getBeanFactory().getBean(requiredType, args);}
    public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {this.assertBeanFactoryActive();return this.getBeanFactory().getBeanProvider(requiredType);}
    public <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType) {this.assertBeanFactoryActive();return this.getBeanFactory().getBeanProvider(requiredType);}
    public boolean containsBean(String name) {return this.getBeanFactory().containsBean(name);}
    public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {this.assertBeanFactoryActive();return this.getBeanFactory().isSingleton(name);}
    public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {this.assertBeanFactoryActive();return this.getBeanFactory().isPrototype(name);}

這裡我們可以發現getBean ()方法很眼熟,因為在最開始學習spring時沒有用spring的腳手架創建項目,我們獲取bean的方法通常是classPathContextLoader掃描bean的xml文件解析組成ApplicationCOntext對象,再調用它的getBean方法獲取實例bean。

由此可以發現我們主要的應用途徑就是使用這個getBean的方法,那麼動態的注入bean我們通過很多方法就能實現,所以這裡不難想到,靜態方法中無法使用注入的bean的問題。

其次我們來復現這個問題,大家來看如下的程式碼:

public class JsonGetter {
@Resource
private UuidGetter uuidGetter;
public static string Test(){
       return uuidGetter.getUuid();
}
public static JsONobject set0bjectToJsonObject(object data){
       return JsoNobject.parseObject(String.valueof(JsONObject.toJSON(data)));
}
public static JsONObject setStringTO3son0bject(String data) { return JsONObject.parseObject(data);
}

我們發現在靜態的Test方法中調用注入的bean直接報錯,這裡解釋一下:歸功於類的載入機制與載入順序,靜態屬性與靜態程式碼區塊先載入(static靜態優先),這裡載入靜態方法是沒有bean實例給你用的,自然會報錯。

如何解決?我們可以採取Spring取得bean物件時呼叫getBean方法的思路,在容器載入時將spring容器的上下文進行靜態儲存:

@Component
@Lazy(value = false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
    /**
     * 将上下文静态设置,在初始化组件时就进行静态上下文的覆盖(这个覆盖是将远spring容器的上下文对象引用加到我们预定设置)
     */
    private static ApplicationContext applicationContext = null;
    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }
    public static  <T> T getBean(Class<T> beanType) {
        assertContextInjected();
        return applicationContext.getBean(beanType);
    }
    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }
    @Override
    public void destroy() {
        applicationContext = null;
    }
    private static void assertContextInjected() {
        Assert.notNull(applicationContext,
                "applicationContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
    }
    public static void pushEvent(ApplicationEvent event){
        assertContextInjected();
        applicationContext.publishEvent(event);
    }
}

這裡只需要關注的是靜態成員變數ApplicationContext的定義、賦值與驗證:

    /**
     * 将上下文静态设置,在初始化组件时就进行静态上下文的覆盖(这个覆盖是将远spring容器的上下文对象引用加到我们预定设置)
     */
    private static ApplicationContext applicationContext = null;

重寫擴充介面的方法,實作靜態上下文的覆寫:

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

將取得它的方法公有修飾,以便共享:

    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

寫到這裡還是不明白,這麼定義一個元件,將spring上下文物件靜態覆蓋到底有何作用?

不要慌,我們來看看這個類別的這個方法:

public class AppContext {
    static transient ThreadLocal<Map<String, String>> contextMap = new ThreadLocal<>();
    ......省略n行业务代码
    public static void fillLoginContext() {
        DingAppInfo appInfo = SpringContextHolder.getBean(DingAppInfoService.class).findAppInfo(APP_CODE);
        setDingVerifyInfo(appInfo);
        CloudChatAppInfo cloudChatAppInfo = SpringContextHolder.getBean(CloudChatAppInfoService.class).findAppInfo(APP_CODE);
        setCloudChatInfo(cloudChatAppInfo);
    }
    public static void clear() {
        contextMap.remove(); //本地线程的remove方法极其重要,注意每次给它使用之后一定要调用remove清理,防止内存泄露。
    }
}

我們發現上例程式碼中進行了查庫的操作:

DingAppInfo appInfo = SpringContextHolder.getBean(DingAppInfoService.class).findAppInfo(APP_CODE);

以上是SpringBoot ApplicationContextAware拓展介面如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除