以下内容是从书中摘录来的,但是我发现即使摘录一遍,对其内容的理解也会更加深入!
[b]一、Spring装配Bean的过程[/b]
1. 实例化;
2. 设置属性值;
3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name;
4. 如果实现BeanFactoryAware接口,调用setBeanFactory 设置BeanFactory;
5. 如果实现ApplicationContextAware,调用setApplicationContext设置ApplicationContext
6. 调用BeanPostProcessor的预先初始化方法;
7. 调用InitializingBean的afterPropertiesSet()方法;
8. 调用定制init-method方法;
9. 调用BeanPostProcessor的后初始化方法;
[b]Spring容器关闭过程[/b]
1. 调用DisposableBean的destroy();
2. 调用定制的destroy-method方法;
[b]总结:[/b]
在Bean初始化的过程中最多有三次修改Bean的机会,实现InitializingBean或者定制init-method是一次机会,区别是一个与SpringFrameWork紧密偶合;实现BeanPostProcessor为Bean的修改提供了两次机会.
如果需要调用BeanFactor的一些特性,如发布事件等需要对BeanFactor或者ApplicationContext进行引用需实现BeanFactoryAware或者ApplicationContextAware.当然如果想得到自己的ID或者名字则实现BeanNameAware.
ApplicationContextAwareProcessor 是BeanPostProcessor的一个实现,将ApplicationContext传递给所有的实现ApplicationContextAware的Bean.
[b]二、依赖注入[/b]
1、set依赖注入
[code] <property><ref bean="beanName"></property>
<property><ref local="beanName"></property>
<property><bean class="beanClass"/></property>
<property>
<list>
<value>bar</value>
<ref bean="foo"/>
</list>
</property>
<property>
<set>
<value>bar</value>
<ref bean="foo"/>
</set>
</property>
<property>
<map>
<entry key="key1"><!--主键只能是String-->
<value>bar</value>
</entry>
<entry key="key2">
<ref bean="foo"/>
</entry>
</map>
</property>
<property>
<prop>
<prop key="key1">foo</value>
<prop key="key2">bar</value>
</prop>
</property>
<property></null></property>[/code]
[b]一、Spring装配Bean的过程[/b]
1. 实例化;
2. 设置属性值;
3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name;
4. 如果实现BeanFactoryAware接口,调用setBeanFactory 设置BeanFactory;
5. 如果实现ApplicationContextAware,调用setApplicationContext设置ApplicationContext
6. 调用BeanPostProcessor的预先初始化方法;
7. 调用InitializingBean的afterPropertiesSet()方法;
8. 调用定制init-method方法;
9. 调用BeanPostProcessor的后初始化方法;
[b]Spring容器关闭过程[/b]
1. 调用DisposableBean的destroy();
2. 调用定制的destroy-method方法;
[b]总结:[/b]
在Bean初始化的过程中最多有三次修改Bean的机会,实现InitializingBean或者定制init-method是一次机会,区别是一个与SpringFrameWork紧密偶合;实现BeanPostProcessor为Bean的修改提供了两次机会.
如果需要调用BeanFactor的一些特性,如发布事件等需要对BeanFactor或者ApplicationContext进行引用需实现BeanFactoryAware或者ApplicationContextAware.当然如果想得到自己的ID或者名字则实现BeanNameAware.
ApplicationContextAwareProcessor 是BeanPostProcessor的一个实现,将ApplicationContext传递给所有的实现ApplicationContextAware的Bean.
[b]二、依赖注入[/b]
1、set依赖注入
[code] <property><ref bean="beanName"></property>
<property><ref local="beanName"></property>
<property><bean class="beanClass"/></property>
<property>
<list>
<value>bar</value>
<ref bean="foo"/>
</list>
</property>
<property>
<set>
<value>bar</value>
<ref bean="foo"/>
</set>
</property>
<property>
<map>
<entry key="key1"><!--主键只能是String-->
<value>bar</value>
</entry>
<entry key="key2">
<ref bean="foo"/>
</entry>
</map>
</property>
<property>
<prop>
<prop key="key1">foo</value>
<prop key="key2">bar</value>
</prop>
</property>
<property></null></property>[/code]