使用SpringContextHolder獲取bean實例


文章轉自:https://blog.csdn.net/chenyiminnanjing/article/details/78618847

今天在一個utils包下的文件中寫一個方法的時候想去使用@autowired注入一些對象,但發現不可以直接使用@Autowired,因為方法是static,方法中使用的對象也必須是static,但正常情況下@Autowired無法注入靜態的bean,於是發現項目中用到了springContextHolder,通過使用

 private static Constants constants = SpringContextHolder.getBean(Constants.class);

這個方法就可以拿到靜態的contants對象,從而獲取到其中的變量等內容。於是就仔細看了看SpringContextHolder。 
下面是完整代碼

/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cmb.bip.utils;

import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時候取出ApplicaitonContext.
 * 
 */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext applicationContext = null;


    /**
     * 取得存儲在靜態變量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

    /**
     * 從靜態變量applicationContext中取得Bean, 自動轉型為所賦值對象的類型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }

    /**
     * 從靜態變量applicationContext中取得Bean, 自動轉型為所賦值對象的類型.
     */
    public static <T> T getBean(Class<T> requiredType) {
        assertContextInjected();
        return applicationContext.getBean(requiredType);
    }

    /**
     * 清除SpringContextHolder中的ApplicationContext為Null.
     */
    public static void clearHolder() {
        applicationContext = null;
    }

    /**
     * 實現ApplicationContextAware接口, 注入Context到靜態變量中.
     */
    @Override
    public void setApplicationContext(ApplicationContext appContext) {
        applicationContext = appContext;
    }

    /**
     * 實現DisposableBean接口, 在Context關閉時清理靜態變量.
     */
    @Override
    public void destroy() throws Exception {
        SpringContextHolder.clearHolder();
    }

    /**
     * 檢查ApplicationContext不為空.
     */
    private static void assertContextInjected() {
        Validate.validState(applicationContext != null, "applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder.");
    }
}

看了看這個好像是跟網上的一樣,應該是一個固定代碼。 
這里正常情況下應該去配置文件配置出SpringContextHolder的bean,比如

<bean id="springContextHolder" class="com.cmb.bip.utils.SpringContextHolder" />  

但我找了一圈發現我們的代碼中沒有,然后發現是我們使用了@Service。同時和這句

<!-- 使用Annotation自動注冊Bean -->
    <context:component-scan base-package="com.cmb.bip"></context:component-scan>

這樣同樣也是注冊了springContextHolder這個bean了。 
有了springContextHolder這個bean后就可以使用其下的getbean方法,從而獲取到constants對象了。 
而對於為何springContextHolder就能夠使用到getbean方法則是因為其繼承了ApplicationContextAware。通過它Spring容器會自動把上下文環境對象調用ApplicationContextAware接口中的setApplicationContext方法。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM