SpringContextHolder 靜態持有SpringContext的引用


import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *
 *以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時候中取出ApplicaitonContext.
 * @author zhuh
 */
public class SpringContextHolder implements ApplicationContextAware{

    private static ApplicationContext applicationContext;

    
    //實現ApplicationContextAware接口的context注入函數, 將其存入靜態變量.
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }

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

    
    //從靜態變量ApplicationContext中取得Bean, 自動轉型為所賦值對象的類型.
    //如果有多個Bean符合Class, 取出第一個.
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        @SuppressWarnings("rawtypes")
                Map beanMaps = applicationContext.getBeansOfType(clazz);
        if (beanMaps!=null && !beanMaps.isEmpty()) {
            return (T) beanMaps.values().iterator().next();
        } else{
            return null;
        }
    }

    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,請在applicationContext.xml中定義SpringContextHolder");
        }
    }

}

<!-- 用於持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的靜態方法得到spring bean對象 -->  

<bean class="com.xxxxx.SpringContextHolder"  />

 

該工具類主要用於:那些沒有歸入spring框架管理的類卻要調用spring容器中的bean提供的工具類。

在spring中要通過IOC依賴注入來取得對應的對象,但是該類通過實現ApplicationContextAware接口,以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時候中取出ApplicaitonContext.

如此就不能說說org.springframework.context.ApplicationContextAware這個接口了:

當一個類實現了這個接口(ApplicationContextAware)之后,這個類就可以方便獲得ApplicationContext中的所有bean。換句話說,就是這個類可以直接獲取spring配置文件中,所有有引用到的bean對象。

除了以上SpringContextHolder類之外,還有不需要多次加載spring配置文件就可以取得bean的類:

1.Struts2框架中,在監聽器中有這么一句
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
之后可以用
scheduleService = (IScheduleService)context.getBean("scheduleService");
取到對象,請問context都可以取到什么信息,這些信息的來源在哪?是XML里配置了呢,還是固定的一部分信息呢?
2、這個 application封裝的是web.xml 內部的信息
而你的web.xml里面有spring的配置文件,所有,里面還包含spring的信息
同樣包含struts2的filter信息
總之就是和web.xml有關系的所有信息

3、在web.xml里有這么一段
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

那么在取信息的時候,也會把applicationContext.xml里的信息取出來






免責聲明!

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



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