官方解释:实现这个接口的bean其实是希望知道自己属于哪一个beanfactory
public class MyBeanFactoryAware implements BeanFactoryAware{
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("I belong to :"+beanFactory);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="mybeanFactoryAware" class="org.study.spring.beanfactoryaware.MyBeanFactoryAware"></bean>
</beans>
test
public class MyBeanFactoryAwareTest{
@Test
public void test2() throws Exception{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-factory-aware.xml");
MyBeanFactoryAware mybeanFactoryAware = applicationContext.getBean("mybeanFactoryAware",MyBeanFactoryAware.class);
}
}