这一节我们还是先了解一下BeanFactoryAware这个接口,之所以说只是了解一下,还是希望等到分析refresh()的时候有个更好的理解吧
照旧先上源代码:
官方解释:实现这个接口的bean其实是希望知道自己属于哪一个beanfactory
言简意赅,不需要做多解释,先实现一下自己,看看他的基本功能吧,看代码:
MyBeanFactoryAware.java
bean-factory-aware.xml
1. package org.study.spring.beanfactoryaware;
2.
3. import org.springframework.beans.BeansException;
4. import org.springframework.beans.factory.BeanFactory;
5. import org.springframework.beans.factory.BeanFactoryAware;
6.
7. public class MyBeanFactoryAware implements BeanFactoryAware{
8.
9.
10. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
11. "I belong to :"+beanFactory);
12. }
13.
14. }
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4. xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
8. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
9. http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
10. >
11.
12.
13.
14. <bean id="mybeanFactoryAware" class="org.study.spring.beanfactoryaware.MyBeanFactoryAware"></bean>
15.
16. </beans>
MyBeanFactoryAwareTest.java
1. package org.study.spring.beanfactoryaware;
2.
3. import org.junit.Test;
4. import org.springframework.context.ApplicationContext;
5. import org.springframework.context.support.ClassPathXmlApplicationContext;
6.
7. public class MyBeanFactoryAwareTest{
8.
9.
10. @Test
11. public void test2() throws Exception{
12. new ClassPathXmlApplicationContext("bean-factory-aware.xml");
13. "mybeanFactoryAware",MyBeanFactoryAware.class);
14.
15. }
16.
17. }
运行结果:
的确,根据我们上次分析的源代码,的确属于DefaultListableBeanFactory
换个说法,我们目前不知道这个接口更多真实的意义,如果不分析其他的部分,我们就知道可以在实现BeanFactoryAware的bean中获取beanfactory,也就是获取上下文,其他的不知道暂时有什么用处,也许分析refresh就会有理解了吧~