对Spring的BeanFactory的学习小节

来源:互联网 发布:linux 进程启动顺序 编辑:程序博客网 时间:2024/05/21 10:56

以下内容是从书中摘录来的,但是我发现即使摘录一遍,对其内容的理解也会更加深入!
一、Spring装配Bean的过程
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的后初始化方法;

Spring容器关闭过程
1. 调用DisposableBean的destroy();
2. 调用定制的destroy-method方法;

总结:
在Bean初始化的过程中最多有三次修改Bean的机会,实现InitializingBean或者定制init-method是一次机会,区别是一个与SpringFrameWork紧密偶合;实现BeanPostProcessor为Bean的修改提供了两次机会.
如 果需要调用BeanFactor的一些特性,如发布事件等需要对BeanFactor或者ApplicationContext进行引用需实现 BeanFactoryAware或者ApplicationContextAware.当然如果想得到自己的ID或者名字则实现 BeanNameAware.
ApplicationContextAwareProcessor 是BeanPostProcessor的一个实现,将ApplicationContext传递给所有的实现ApplicationContextAware的Bean.

二、依赖注入
1、set依赖注入

代码
  1.  <property><ref bean="beanName"></property>  
  2. <property><ref local="beanName"></property>  
  3. <property><bean class="beanClass"/></property>  
  4. <property>  
  5.     <list>  
  6.         <value>bar</value>  
  7.         <ref bean="foo"/>  
  8.     </list>  
  9. </property>  
  10. <property>  
  11.     <set>  
  12.         <value>bar</value>  
  13.         <ref bean="foo"/>  
  14.     </set>  
  15. </property>  
  16. <property>  
  17.     <map>  
  18.         <entry key="key1"><!--主键只能是String-->  
  19.             <value>bar</value>  
  20.         </entry>  
  21.         <entry key="key2">  
  22.             <ref bean="foo"/>  
  23.         </entry>  
  24.     </map>  
  25. </property>  
  26. <property>  
  27.     <prop>  
  28.         <prop key="key1">foo</value>  
  29.         <prop key="key2">bar</value>  
  30.     </prop>  
  31. </property>       
  32. <property></null></property>  

 

2、constructor注入

代码
  1. <constructor-arg>  
  2.    <value>42</value>  
  3. </constructor-arg>  
  4. <constructor-arg>  
  5.    <bean ref="foo"/>  
  6. </constructor-arg>  
  7. <constructor-arg index="0">  
  8.    <bean ref="foo"/>  
  9. </constructor-arg>  
  10. <constructor-arg type="java.net.URL">  
  11.    <value>http://abc.com</value>  
  12. </constructor-arg>  

 

 

三、自动装配
<bean id="foo" class="beanClass" autowire="autowire type"/>
有四种自动装配类型,byName,byType,constructor,autodetect,前两个是针对Set的自动装配,autodetect是由容器选择,在装配不成功(有重复情况)容器会抛异常而不是最先匹配原则。
<beans default-autowire="byName"/>缺省无自动装配,可设置缺省装配属性。
自动装配和非自动装配可混合使用。

 

四、BeanFactorPostProcessor
在BeanFactory 载入所有Bean后,实例化Bean前,对BeanFactor做一些后处理工作,PropertyPlaceholderConfiger和 CustomEditorConfigurer是SpringFrameWork自定义的BeanFactoryPostProcessor.

PropertyPlaceholderConfiger

代码
  1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfiger">  
  2.    <property name="location">jdbc.properties</property>  
  3. </bean>  
  4. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfiger">  
  5.    <property name="locations">  
  6.       <list>  
  7.          <value>jdbc.properties</value>  
  8.          <value>security.properties</value>  
  9.       </list>  
  10. </bean>  
  11. <bean id="" class="">  
  12.     <property>  
  13.       <value>${database.url}</value>  
  14.     </property>  
  15. </bean>  

 

CustomerEditorConfigurer
继承java.beans.PropertyEditorSupport实现自己的TypeEditor,实现其中的setAsText方法,代码事例

代码
  1. public class PhoneEditor extends java.beans.   
  2. PropertyEditorSupport{   
  3.     public void setAsText(String textvalue)   
  4.     {   
  5.         ......   
  6.         PhoneNumber phone = new PhoneNumber();   
  7.         setValue(phone);   
  8.     }   
  9. }  

 

注册到SpringFrameWork

代码
  1. <bean id="customerEditorConfigurer" class="org.springframework.beans.factory.config.CustomerEditorConfiger">  
  2.   <property name="customEditors">  
  3.      <map>  
  4.        <entry key="com.Phone"><!---->  
  5.           <bean id="phoneEditor" class="PhoneEditor"/>  
  6.        </entry>     
  7.      </map>  
  8.   </property>  
  9. </bean>  

 

五、Event
系统事件ContextClosedEvent,ContextRefreshedEvent(初始化或者刷新时),RequestHandlerEvent Web请求被处理后。

接收事件的Bean 需实现ApplicationListener.

继承ApplicationEvent实现自己的Event,后由Bean调用context.publishEvent()发布事件,当然前提是你的Bean 要通过实现ApplicatonContextAware获得ApplicationContext.

 
原创粉丝点击