BeanNameAware,BeanFactoryAware, ApplicationContextAware

对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让Bean知道在容器中是以那个名称被管理的,或者让Bean知道BeanFactory或者ApplicationContext的存在,也就是产让该Bean可以取得BeanFactory或者ApplicationContext的实例,如果Bean可以意识到这些对象,那么就可以在Bean的某些动作发生时,做一些如事件发布等操作。

beanNameAware接口:如果某个bean需要访问配置文件中本身bean的id属性,这个Bean类通过实现该接口,在依赖关系确定之后,初始化方法之前,提供回调自身的能力,从而获得本身bean的id属性,该接口提供了void setBeanName(String name)方法实现,需要指出的是该方法的name参数就是该bean的id属性,加调该setBeanName方法可以让bean获取得自身的id属性

BeanFactoryAware接口:实现了BeanFactoryAware接口的bean,可以直接通过beanfactory来访问spring的容器,当该bean被容器创建以后,会有一个相应的beanfactory的实例引用,该 接口有一个方法void setBeanFactory(BeanFactory beanFactory)方法通过这个方法的参数创建它的BeanFactory实例,实现了BeanFactoryAware接口,就可以让Bean拥有访问Spring容器的能力。缺点:导致代码与spring的api耦合在一起,这种方式不推荐。

ApplicationContextAware接口:在Bean类被初始化后,将会被注入applicationContext实例,该接口有一个方法,setApplicationContext(ApplicationContext context),使用其参数context用来创建它的applicationContext实例,缺点:导致代码与spring的api耦合在一起,这种方式不推荐。

 

BeanNameAware 和 BeanFactoryAware

定义一个bean实现BeanNameAware,BeanFactoryAware这两个接口

public class HelloSpring implements BeanNameAware, BeanFactoryAware {
    private BeanFactory bf;
    public void init(){
        System.out.println("正在执行初始化方法init");
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.bf=beanFactory;

    }
    @Override
    public void setBeanName(String s) {
        System.out.println("回调setBeanName方法  id属性是"+s);
    }

    public BeanFactory getBf() {
        return bf;
    }
}

将bean加入spring容器中

@Configuration
public class MainConfig {
    @Bean(initMethod = "init")
    public HelloSpring helloWorld(){
        System.out.println("add bean helloWorld into spring");
        return new HelloSpring();
    }
}

开启spring容器

@Test
    public void test07(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        HelloSpring helloWorld = applicationContext.getBean(HelloSpring.class);
        System.out.println("得到beanFactory对象 "+helloWorld.getBf());
    }

测试结果如下

ApplicationContextAware问题背景

在我们的web程序中,用spring来管理各个实例(bean), 有时在程序中为了使用已被实例化的bean, 通常会用到这样的代码:

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person = (Person)applicationContext.getBean("person");

 

但是这样就会存在一个问题:因为它会重新装载并实例化上下文bean,如果有些线程配置类也是在这个配置文件中,那么会造成做相同工作的的线程会被启两次。一次是web容器初始化时启动,另一次是上述代码显示的实例化了一次。当于重新初始化一遍!!!!这样就产生了冗余。

解决方法

不用类似new AnnotationConfigApplicationContext(MainConfig.class)

的方式,从已有的spring上下文取得已实例化的bean。通过ApplicationContextAware接口进行实现。

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。

ApplicationContextAware应用实例

定义一个SpringContextHolder类

public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    /**
     * 获取静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

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

假如现在在一个不在spring管理范围的类需要引用容器中的bean,可以通过如下的方式完成。

    public class CacheUtils {
    	private static CacheManager cacheManager = ((CacheManager) SpringContextHolder.getBean("cacheManager"));
    }