最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用靜態(tài)方法獲取spring的bean實(shí)例

 更新時間:2025年11月10日 09:27:08   作者:水煮魚又失敗了  
文章介紹了如何在Spring框架中通過工具類的靜態(tài)方法獲取Spring命名空間中的bean,思路包括定義bean并實(shí)現(xiàn)ApplicationContextAware接口,然后在代碼中執(zhí)行特定代碼獲取bean,擴(kuò)展部分詳細(xì)講解了加載原理和注意事項(xiàng)

1 場景

spring命名空間中的bean,正常情況下可以使用@Autoware注解加在成員變量上注入,注入成功的前提是注入的對象必須已經(jīng)是spring命名空間中的bean才可以。

當(dāng)前有一種需求:通過工具類的靜態(tài)方法,獲取spring中的bean

2 思路

(1)定義bean

(2)bean實(shí)現(xiàn)ApplicationContextAware接口

3 代碼

3.1 定義bean

/**
 * spring上下文句柄
 */
public class SpringContextHolder implements ApplicationContextAware {
    /**
     * spring上下文
     */
    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext=applicationContext;
    }
    
    /**
     * 獲取spring上下文
     * @return
     */
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
    
    /**
     * 獲取bean
     * @param name  bean名稱
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name){
        if(applicationContext==null){
            return null;
        }
        return (T) applicationContext.getBean(name);
    }
    
    /**
     * 獲取bean
     * @param requiredType bean類型
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> requiredType){
        if(applicationContext==null){
            return null;
        }
        return applicationContext.getBean(requiredType);
    }
    
    /**
     * 獲取bean
     * @param name  bean名稱
     * @param requiredType  bean類型
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> requiredType){
        if(applicationContext==null){
            return null;
        }
        return applicationContext.getBean(name,requiredType);
    }
}

3.2 注冊bean

spring配置文件中,加載的xml最前面定義上述bean:

<!-- spring上下文句柄 -->
<bean id="springContextHolder" class="com.sa.demo.spring.context.SpringContextHolder"/>

3.3 使用

在java代碼的任何一個地方執(zhí)行下面代碼,可以獲取spring中的bean:

SpringContextHolder.getBean("xxxxxx");
SpringContextHolder.getBean("XXX.class")
SpringContextHolder.getBean("xxxxxx","XXX.class")

獲取spring中的命名空間:

ApplicationContext applicationContext=SpringContextHolder.getApplicationContext();

4 擴(kuò)展

4.1 源碼講解

加載原理:

實(shí)現(xiàn)的接口ApplicationContextAware中的方法定義如下:

/**
 * Set the ApplicationContext that this object runs in.
 * Normally this call will be used to initialize the object.
 * <p>Invoked after population of normal bean properties but before an init callback such
 * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
 * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
 * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
 * {@link MessageSourceAware}, if applicable.
 * @param applicationContext the ApplicationContext object to be used by this object
 * @throws ApplicationContextException in case of context initialization errors
 * @throws BeansException if thrown by application context methods
 * @see org.springframework.beans.factory.BeanInitializationException
 */
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

翻譯如下:

設(shè)置運(yùn)行該對象的ApplicationContext。
通常這個調(diào)用將用于初始化對象。
在填充普通bean屬性之后,在init回調(diào)之前調(diào)用

表示實(shí)現(xiàn)此接口的bean,將在填充bean屬性之后,bean的init回調(diào)方法之前,調(diào)用此方法。方法的參數(shù)為spring的命名空間ApplicationContext

bean實(shí)現(xiàn)此接口后,會被調(diào)用相應(yīng)方法,取決于以下源碼:

方法路徑:

org.springframework.context.support.ApplicationContextAwareProcessor#postProcessBeforeInitialization

org.springframework.context.support.ApplicationContextAwareProcessor#invokeAwareInterfaces

對應(yīng)方法如下:

private void invokeAwareInterfaces(Object bean) {
    if (bean instanceof EnvironmentAware) {
        ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
    }
    if (bean instanceof EmbeddedValueResolverAware) {
        ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
    }
    if (bean instanceof ResourceLoaderAware) {
        ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
    }
    if (bean instanceof ApplicationEventPublisherAware) {
        ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
    }
    if (bean instanceof MessageSourceAware) {
        ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
    }
    if (bean instanceof ApplicationContextAware) {
        ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
    }
}

相應(yīng)擴(kuò)展部分:

當(dāng)spring中注冊的bean實(shí)現(xiàn)了如下接口:

EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationContextAware

均會執(zhí)行上述代碼中對應(yīng)的實(shí)現(xiàn)方法。

4.2 注意事項(xiàng)

(1)此類bean的定義,需在spring最前面定義。

(2)實(shí)現(xiàn)了接口ApplicationContextAware的bean,只能獲取此bean所定義的spring命名空間。

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

佛教| 开封市| 商洛市| 连城县| 双鸭山市| 霍林郭勒市| 鸡东县| 方正县| 日照市| 山阴县| 图片| 龙岩市| 通道| 图木舒克市| 太保市| 兴城市| 阿克| 铜陵市| 永安市| 南昌市| 和平县| 冀州市| 阿合奇县| 甘德县| 马公市| 什邡市| 牡丹江市| 额敏县| 安溪县| 曲麻莱县| 连平县| 时尚| 公安县| 湖口县| 海兴县| 夏河县| 湘潭市| 淮南市| 上林县| 平顺县| 阳西县|