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

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

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

1 場(chǎng)景

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

當(dāng)前有一種需求:通過(guò)工具類的靜態(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 注冊(cè)bean

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

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

3.3 使用

在java代碼的任何一個(gè)地方執(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)行該對(duì)象的ApplicationContext。
通常這個(gè)調(diào)用將用于初始化對(duì)象。
在填充普通bean屬性之后,在init回調(diào)之前調(diào)用

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

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

方法路徑:

org.springframework.context.support.ApplicationContextAwareProcessor#postProcessBeforeInitialization

org.springframework.context.support.ApplicationContextAwareProcessor#invokeAwareInterfaces

對(duì)應(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中注冊(cè)的bean實(shí)現(xiàn)了如下接口:

EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationContextAware

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

4.2 注意事項(xiàng)

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

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

總結(jié)

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

相關(guān)文章

最新評(píng)論

宁城县| 定南县| 隆回县| 古田县| 彭州市| 开化县| 凤阳县| 乌拉特前旗| 宁蒗| 大城县| 玛曲县| 象山县| 万荣县| 阿合奇县| 旅游| 互助| 怀安县| 思南县| 江门市| 娄烦县| 岳普湖县| 阿克苏市| 信宜市| 繁昌县| 武邑县| 正阳县| 涡阳县| 合川市| 久治县| 榆林市| 肥东县| 两当县| 亳州市| 五家渠市| 黎平县| 房产| 新郑市| 扶风县| 镇远县| 普格县| 靖江市|