Spring2.0简明手册(系列之一 Bean的配置及使用)
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://robert.blog.51cto.com/374512/74844 |
这一系列文章是对Spring Framework 开发参考手册的简要概述,个别地方会酌情扩展,以便随时查阅。 主要参考资料:http://www.redsaga.com/spring_ref/2.0/html/ 第Ⅰ部分:核心技术 1.实例化 BeanFactory的实例化 Spring参考手册->3.2.2. 实例化容器 ApplicationContext实例化 以声明的方式创建,通过ContextLoaderListener或ContextLoaderServlet,首选ContextLoaderListener。 以编程方式创建: ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml"); Bean的实例化 Spring参考手册->3.2.3.2.1. 用构造器来实例化 构造器的参数由constructor-arg元素提供 Spring参考手册->3.2.3.2.2. 使用静态工厂方法实例化 静态工厂方法的参数由constructor-arg元素提供 Spring参考手册->3.2.3.2.3. 使用实例工厂方法实例化 实例工厂方法的参数由constructor-arg元素提供 2.依赖 依赖注入(DI) DI主要有两种注入方式,即Setter注入和 构造器注入 Bean的实例化和依赖注入的例子 Spring参考手册->3.3.1.3. 一些例子 Setter注入 构造器注入 采用静态工厂方法返回对象实例 非静态的实例工厂方法 constructor-arg 使用index属性可以显式的指定构造器参数出现顺序,这是使用构造器IoC首选的方式。 <constructor-arg/>及<property/>元素的使用详解 在<constructor-arg/>或<property/>元素内部可以使用以下元素: 1)<value/>元素 直接量(基本类型、Strings类型等。) 2)idref元素 idref元素用来将容器内其它bean的id传给<constructor-arg/> 或 <property/>元素,同时提供错误验证功能。 3)ref元素 有三种不同的形式(详见:Spring参考手册->3.3.3.2. 引用其它的bean(协作者)) ①<ref bean="someBean"/> ②<ref local="someBean"/> ③<ref parent="accountService"/> 注意:内部bean中的singleton标记及id或name属性将被忽略。内部bean总是匿名的且它们总是prototype模式的。 5)<list/>、<set/>、<map/>及<props/>元素 集合合并 仅在Spring 2.0(及随后的版本中)可用,不同的集合类型是不能合并(如map和 list是不能合并的),merge属性必须在继承的子bean中定义,而在父bean的集合属性上指定的merge属性将被忽略。 强类型集合(仅适用于Java5+) 6)<null/> 简化格式 <property name="myProperty" value="hello"/> <constructor-arg value="hello"/> <entry key="myKey" value="hello"/> <property name="myProperty" ref="myBean"/> <constructor-arg ref="myBean"/> <entry key-ref="myKeyBean" value-ref="myValueBean"/> <property name="fred.bob.sammy" value="123" /> 使用depends-on depends-on属性可以用于当前bean初始化之前显式地强制一个或多个bean被初始化。 延迟初始化bean 通过<bean/>元素中的lazy-init属性 在<beans/>元素上使用'default-lazy-init'属性 自动装配 <bean/>元素中使用autowire属性:no/byName/byType/constructor 如果直接使用property和constructor-arg注入依赖的话,那么将总是覆盖自动装配。 推荐在开发过程中采用自动装配,而在系统趋于稳定的时候改为显式装配的方式。 尽管使用autowire没有对错之分,但是能在一个项目中保持一定程度的一致性是最好的做法。 <bean/>元素的 autowire-candidate属性可被设为false,这样容器在查找自动装配对象时将不考虑该bean。 依赖检查 <bean/>元素中使用dependency-check属性:none/simple/object/all 方法注入 Lookup方法注入 <lookup-method name="createCommand" bean="command"/> 3.bean的作用域 Singleton作用域 <bean id="accountService" class="com.foo.DefaultAccountService"/> <bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/> DOCTYPE: spring-beans-2.0.dtd 或 spring-beans-2.0.xsd<bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/> Prototype作用域 根据经验,对所有有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。 <bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/> <bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/> 其他作用域(仅在基于web的Spring ApplicationContext情形下有效) 初始化web配置 如果你使用的是Servlet 2.4及以上的web容器,需要在web.xml中配置<listener> 如果你用的是早期版本的web容器(Servlet 2.4以前),需要在web.xml中配置<filter> Request作用域 <bean id="loginAction" class="com.foo.LoginAction" scope="request"/> <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/> <bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/> 在作用域bean定义里插入一个<aop:scoped-proxy/>子元素 自定义作用域 Spring参考手册->3.4.4. 自定义作用域 4.定制bean特性 初始化回调 1)实现InitializingBean接口<通常,要避免使用InitializingBean接口(而且不鼓励使用该接口,因为这样会将代码和Spring耦合起来)> 2)在XML配置文件中指定init-method属性 析构回调 1)实现DisposableBean接口<通常,要避免使用DisposableBean标志接口(而且不鼓励使用该接口,因为这样会将代码与Spring耦合在一起)> 2)在XML配置文件中指定destroy-method属性 缺省的初始化和析构方法 在<beans/>元素上使用'default-init-method'属性 在<beans/>元素上使用'default-destroy-method'属性 BeanFactoryAware接口 BeanNameAware接口 5.bean定义的继承 一般父bean被定义为abstract,子bean通过parent属性指向父bean。 6.IoC容器的扩展 用BeanPostProcessor定制bean 用BeanFactoryPostProcessor定制配置元数据 PropertyPlaceholderConfigurer示例 PropertyOverrideConfigurer示例 使用FactoryBean定制实例化逻辑 7.ApplicationContext 利用MessageSource实现国际化 ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能 当一个ApplicationContext被加载时,它会自动在context中查找已定义为MessageSource类型的bean。此bean的名称须为messageSource。 Spring目前提供了两个MessageSource的实现:ResourceBundleMessageSource和StaticMessageSource。ResourceBundleMessageSource会用得更多一些 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 通过ApplicationEvent类和ApplicationListener接口 调用ApplicationContext的publishEvent()方法,且指定一个实现了ApplicationEvent的自定义事件类实例做参数。 本文出自 “点滴积累_勤耕不辍” 博客,请务必保留此出处http://robert.blog.51cto.com/374512/74844 本文出自 51CTO.COM技术博客 |


robert.luo77
博客统计信息
热门文章
最新评论
友情链接