Spring——Aware相关接口

Aware,是感应和感知的意思。当bean实现了对应的Aware接口时,BeanFactory会在生产bean时根据它所实现的Aware接口,给bean注入对应的属性,从而让bean获取外界的信息。

Spring提供了一堆Aware接口:
在这里插入图片描述

下面列出几个主要Aware接口作用:

org.springframework.context.ApplicationContextAware接口:

实现类的实例将会获取ApplicationContext的引用,因此可以编程式的使用ApplicationContext手动创建bean.

public interface ApplicationContextAware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

自Spring2.5起,可使用自动装配模式获取ApplicationContext引用:

@RestController
public class HelloWorldController2 {
    @Autowired
    ApplicationContext applicationContext;

    @RequestMapping("/helloworld")
    public String helloWorld() {
        System.out.println("通过applicationContext获取的bean实例:"
                + applicationContext.getBean("helloService1"));
        return "Hello World!";
    }
}

org.springframework.beans.factory.BeanNameAware接口:

在bean内部,它并不知道容器给自己取了个什么id,如果想要获取自己在容器中的id,可以实现BeanNameAware接口获取。其setBeanName(string name)方法的参数就是容器为该bean注入的它本身的id。

public interface BeanNameAware {
    void setBeanName(string name) throws BeansException;
}

org.springframework.beans.factory.BeanFactoryAware 接口:

实现类的实例将会获取BeanFactoryAware的引用。BeanFactoryAware 接口中只有一个方法setBeanFactory(BeanFactory beanFactory)。用法和ApplicationContextAware类似。

举个栗子:

现在新建一个AwareTest类,实现了BeanNameAware和ApplicationContextAware接口,代码如下:

package twm.spring.LifecycleTest;

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;

public class AwareTest implements BeanNameAware, ApplicationContextAware,
        BeanFactoryAware {
    String beanname;
    ApplicationContext appct;
    BeanFactory bFactory;

    @Override
    public void setBeanName(String name) {
        this.beanname = name;
        System.out.println("通过BeanNameAware接口的实现,我知道我的名字是:" + this.beanname);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.appct = applicationContext;
        System.out.println("通过ApplicationContextAware接口实现,获得了容器对象:"
                + this.appct.toString().substring(0, 35) +"......");
        footballPlayer player = appct.getBean("messi", footballPlayer.class);
        player.setName("maladona");
        player.pass();
        player.shoot();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.bFactory = beanFactory;
        System.out.println("通过BeanFactoryAware接口实现,获得了容器对象:"
                + this.bFactory.toString().substring(0, 35)+"......");
    }
}

beans.xml:

<bean id="messi" class="twm.spring.LifecycleTest.footballPlayer">
    <property name="name" value="Messi"></property>
    <property name="team" value="Barcelona"></property>
</bean> 
<bean id="aware_suibianqu" class="twm.spring.LifecycleTest.AwareTest" />

在程序中调用:new ClassPathXmlApplicationContext(“beans.xml”);初始化容器和bean时,输出结果:

通过BeanNameAware接口的实现,我知道我的名字是:aware_suibianqu 
通过BeanFactoryAware接口实现,获得了容器对象:org.springframework.beans.factory.s…… 
通过ApplicationContextAware接口实现,获得了容器对象:org.springframework.context.support…… 
maladona边路传中 maladona射门

猜你喜欢

转载自blog.csdn.net/m0_37556444/article/details/83113452