`
anyeeye
  • 浏览: 144069 次
  • 来自: ...
社区版块
存档分类
最新评论

对Spring的BeanFactory的学习小节

阅读更多
以下内容是从书中摘录来的,但是我发现即使摘录一遍,对其内容的理解也会更加深入!
一、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依赖注入


代码
<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> 





最后更新:2006-12-16 19:07
10:30  |   永久链接  |   浏览 (874)  |   评论 (4)  |    收藏  |   Spring  |   进入论坛  |    

永久链接
http://jamesby.iteye.com/blog/39284 

评论    共 4 条  发表评论 

jamesby     2006-12-16 10:40
2、constructor注入


代码
<constructor-arg> 
   <value>42</value> 
</constructor-arg> 
<constructor-arg> 
   <bean ref="foo"/> 
</constructor-arg> 
<constructor-arg index="0"> 
   <bean ref="foo"/> 
</constructor-arg> 
<constructor-arg type="java.net.URL"> 
   <value>http://abc.com</value> 
</constructor-arg> 




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


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

PropertyPlaceholderConfiger


代码
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfiger"> 
   <property name="location">jdbc.properties</property> 
</bean> 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfiger"> 
   <property name="locations"> 
      <list> 
         <value>jdbc.properties</value> 
         <value>security.properties</value> 
      </list> 
</bean> 
<bean id="" class=""> 
    <property> 
      <value>${database.url}</value> 
    </property> 
</bean> 


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


代码
public class PhoneEditor extends java.beans.  
PropertyEditorSupport{  
    public void setAsText(String textvalue)  
    {  
        ......  
        PhoneNumber phone = new PhoneNumber();  
        setValue(phone);  
    }  



注册到SpringFrameWork


代码
<bean id="customerEditorConfigurer" class="org.springframework.beans.factory.config.CustomerEditorConfiger"> 
  <property name="customEditors"> 
     <map> 
       <entry key="com.Phone"><!----> 
          <bean id="phoneEditor" class="PhoneEditor"/> 
       </entry>    
     </map> 
  </property> 
</bean> 



jamesby     2006-12-16 11:53
五、Event
系统事件ContextClosedEvent,ContextRefreshedEvent(初始化或者刷新时),RequestHandlerEvent Web请求被处理后。

接收事件的Bean 需实现ApplicationListener.

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

完。

分享到:
评论

相关推荐

    Spring的IoC容器(《Spring揭秘》的精选版)

    迷你书是《Spring揭秘》的精选版,节选了原书中介绍Spring IoC容器的6个章节。《Spring揭秘》以幽默生动的语言、辅以有趣的故事和典故,循循善诱地阐述了Spring框架的方方面面。针对Spring框架的主要功能...6.3 小结

    spring in action英文版

    第一部分 Spring基础  第1章 开始Spring之旅  1.1 为什么使用Spring  1.1.1 J2EE开发者的一天  1.1.2 Spring的承诺  1.2 Spring是什么 ... 1.3 开始Spring之旅 ... 1.6 Spring比较 ... 11.6 小结

    Spring.3.x企业应用开发实战(完整版).part2

    1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 ...

    Spring in Action(第2版)中文版

    目录 第一部分spring的核心 第1章开始spring之旅 1.1spring是什么 1.2开始spring之旅 1.3理解依赖注入 ...1.5小结 ...2.1.1beanfactory介绍 ...2.6小结 ...3.3注入非springbean ...3.5使用spring的特殊bean ...b.4小结

    Spring in Action(第二版 中文高清版).part2

    第一部分 Spring的核心 第1章 开始Spring之旅 1.1 Spring是什么 1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP ...B.4 小结

    Spring in Action(第二版 中文高清版).part1

    第一部分 Spring的核心 第1章 开始Spring之旅 1.1 Spring是什么 1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP ...B.4 小结

    Spring3.x企业应用开发实战(完整版) part1

    1.8 小结 第2章 快速入门 2.1 实例功能概述 2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 ...

    Spring中文帮助文档

    14.5.2. 小结 14.6. 文档视图(PDF/Excel) 14.6.1. 简介 14.6.2. 配置和安装 14.7. JasperReports 14.7.1. 依赖的资源 14.7.2. 配置 14.7.3. 构造ModelAndView 14.7.4. 使用子报表 14.7.5. 配置Exporter的...

    Spring API

    14.5.2. 小结 14.6. 文档视图(PDF/Excel) 14.6.1. 简介 14.6.2. 配置和安装 14.7. JasperReports 14.7.1. 依赖的资源 14.7.2. 配置 14.7.3. 构造ModelAndView 14.7.4. 使用子报表 14.7.5. 配置Exporter的...

    Spring官网阅读(十三)ApplicationContext详解(下)

    在前面两篇文章中,我们已经对ApplicationContext的大部分内容做了介绍,包括国际化,Spring中的运行环境,Spring中的资源,Spring中的事件监听机制,还剩唯一一个BeanFactory相关的内容没有介绍,这篇文章我们就来...

    Java Web程序设计教程

    程序设计教程.pdf&gt;&gt;人民邮电出版社的教程哦,所以,好书,你懂的!! 第1章web应用开发简介 1 1.1何为web应用 1 1.1.1web的概念及发展 1 1.1.2web应用程序 2 1.2使用java开发web应用 3 ...本章小结 342

    低清版 大型门户网站是这样炼成的.pdf

    1.5 小结 32 第2章 mvc混血宠儿struts 2 33 2.1 初识mvc新秀struts 2 33 2.1.1 mvc概述 33 .2.1.2 struts 2的mvc实现 35 2.1.3 struts 2的基本组成 36 2.1.4 struts 2的常用类介绍 38 2.1.5 struts 2的业务...

    J2EE应用开发详解

    246 14.5.2 CORBA和RMI的互操作 247 14.6 小结 248 第15章 Spring框架 249 15.1 Spring 2.0的体系结构 249 15.2 Ioc容器 250 15.2.1 BeanFactory 250 15.2.2 ApplicationContext 252 15.2.3 Beans的生命周期过程 253...

Global site tag (gtag.js) - Google Analytics