Spring源码学习--Aware相关接口(beanNameAware接口/BeanFactoryAware接口/ApplicationContextAware接口)

文章来源:

https://www.cnblogs.com/liunanjava/p/4401089.html
https://blog.csdn.net/HaydenYu/article/details/76273509

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

这里写图片描述

注意:除了通过实现Aware结尾接口获取spring内置对象,也可以通过@Autowired注解直接注入相关对象,如下:
(如果需要用到静态方法中,如工具方法,还是采用实现接口的方式)

@Autowired
private MessageSource messageSource; 

@Autowired
private ResourceLoader resourceLoader; 

@Autowired
private ApplicationContext applicationContext;

一 Spring提供一些Aware接口

1 beanNameAware接口:如果某个bean需要访问配置文件中本身bean的id属性,这个Bean类通过实现该接口,在依赖关系确定之后,初始化方法之前,提供回调自身的能力,从而获得本身bean的id属性,该接口提供了

void setBeanName(String name);

方法实现,需要指出的是该方法的name参数就是该bean的id属性,加调该setBeanName方法可以让bean获取得自身的id属性

2 BeanFactoryAware接口:实现了BeanFactoryAware接口的bean,可以直接通过beanfactory来访问spring的容器,当该bean被容器创建以后,会有一个相应的beanfactory的实例引用,该 接口有一个方法

void setBeanFactory(BeanFactory beanFactory) throws BeansException;

通过这个方法的参数创建它的BeanFactory实例,实现了BeanFactoryAware接口,就可以让Bean拥有访问Spring容器的能力。缺点:导致代码与spring的api耦合在一起,这种方式不推荐。

3 ApplicationContextAware接口:在Bean类被初始化后,将会被注入applicationContext实例,该接口有一个方法,

void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

使用其参数context用来创建它的applicationContext实例,缺点:导致代码与spring的api耦合在一起,这种方式不推荐。

二 beanNameAware接口

package com.pb.entity;

import org.springframework.beans.factory.BeanNameAware;
/*
 * 实体类实现init方法和BeanNameAware接口
 */
public class Hello implements BeanNameAware{

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

    }
    public void init(){
        System.out.println("正在执行初始化方法init");
    }

}

applicationContext.xml

<bean id="hello" class="com.pb.entity.Hello" init-method="init"></bean>

测试类:

package com.pb.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pb.entity.Hello;

public class HelloTest {

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello=context.getBean("hello",Hello.class);

    }

}

结果:

回调setBeanName方法  id属性是hello
正在执行初始化方法init

三 BeanFactoryAware接口

package com.pb.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
/*
 * 实体类实现init方法和BeanNameAware接口
 */
public class Hello implements BeanNameAware,BeanFactoryAware{
    private BeanFactory bf;

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

    public void init(){
        System.out.println("正在执行初始化方法init");
    }

    /*
     * 重写setBeanFactory方法
     * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
     */
    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        this.bf=arg0;

    }

    public BeanFactory getBf() {
        return bf;
    }
}

applicationContext.xml

<bean id="hello" class="com.pb.entity.Hello" init-method="init"></bean>

测试类:

package com.pb.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pb.entity.Hello;

public class HelloTest {

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello=context.getBean("hello",Hello.class);
        System.out.println("得到beanFactory对象 "+hello.getBf());

    }

}

结果:

回调setBeanName方法  id属性是hello
正在执行初始化方法init
得到beanFactory对象 org.springframework.beans.factory.support.DefaultListableBeanFactory@3dc0bb: defining beans [hello]; root of factory hierarchy

四 ApplicationContextAware接口

package com.pb.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/*
 * 实体类实现init方法和BeanNameAware接口
 */
public class Hello implements BeanNameAware,BeanFactoryAware,ApplicationContextAware{
    private BeanFactory bf;

    private ApplicationContext context;

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

    }

    public void init(){
        System.out.println("正在执行初始化方法init");
    }

    /*
     * 重写setBeanFactory方法
     * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
     */
    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        this.bf=arg0;

    }

    public BeanFactory getBf() {
        return bf;
    }

    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
        this.context=arg0;

    }

    public ApplicationContext getContext() {
        return context;
    }

}

applicationContext.xml

<bean id="hello" class="com.pb.entity.Hello" init-method="init"></bean>

测试类

package com.pb.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pb.entity.Hello;

public class HelloTest {

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello=context.getBean("hello",Hello.class);
        System.out.println("得到beanFactory对象 "+hello.getBf());
        System.out.println("得到的applicationContext对象:"+hello.getContext());

    }

}

结果:

回调setBeanName方法  id属性是hello
正在执行初始化方法init
得到beanFactory对象 org.springframework.beans.factory.support.DefaultListableBeanFactory@3dc0bb: defining beans [hello]; root of factory hierarchy
得到的applicationContext对象:org.springframework.context.support.ClassPathXmlApplicationContext@1d04653: startup date [Wed Apr 08 00:43:06 CST 2015]; root of context hierarchy

猜你喜欢

转载自blog.csdn.net/u013412772/article/details/80689726