ApplicationContextAware接口的作用

        加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的public void setApplicationContext(ApplicationContext context) throws BeansException方法,获得ApplicationContext对象。

        前提必须在Spring配置文件中指定该类。

/**
 * 以静态变量保存Spring ApplicationContext,可在任意代码中取出ApplicaitonContext.
 * 
 */
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, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        return (T) applicationContext.getBeansOfType(clazz);
    }

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

        同时需要在xml里定义这个bean,这样Spring在创建完ApplicationContext才会调用setApplicationContext(ApplicationContext applicationContext) 注入到类中,对于一些静态类由于无法使用注入而通过SpringContextHolder可以很方便的访问bean实例。

<bean id="springContextHolder" class="xxx.xxx.xxx.SpringContextHolder" lazy-init="false" />

 

PS:加载Spring配置文件,最常用的办法就是用ClassPathXmlApplicationContext,FileSystemClassPathXmlApplicationContext,FileSystemXmlApplicationContext等对象去,但是在加载Spring配置文件的时候,就会生成一个新的ApplicaitonContext对象而不是Spring容器帮我们生成的哪一个, 这样就产生了冗余,所以不采用应用程序手动加载文件的方式,而是使用ApplicationContextAware让Spring容器自动传递自己生成的 ApplicationContext到指定的类里,将来可以通过该类方便的访问spring的上下文。

posted on 2017-04-10 23:00  bijian1013  阅读(497)  评论(0)    收藏  举报

导航