1
、1.
配置文件的简化
1
)
属性的
<property name="foo">
<value>fooValue</value>
</ property >
简化为
<property name="foo" value="fooValue"/>
<value>fooValue</value>
</ property >
简化为
<property name="foo" value="fooValue"/>
2
)
2.引用 bean
<
property
name
="foo">
< ref bean ="fooBean">
</ property >
简化为
< property name ="foo" ref ="fooBean"/>
< ref bean ="fooBean">
</ property >
简化为
< property name ="foo" ref ="fooBean"/>
3
)
3.多属性
<
property
name
="myFriendList">
字串
5
< list >
< value >gigix</ value >
< value> wuyu</ value >
</ list >
</ property >
简化为
< property name ="myFriendList" value ="gigix wuyu"/>
< list >
< value >gigix</ value >
< value> wuyu</ value >
</ list >
</ property >
简化为
< property name ="myFriendList" value ="gigix wuyu"/>
4
)
4.Spring 2.0
<
bean
id
="customerDAO" class="org.springside...CustomerDAO">
< property name ="maxCount" value ="10">
</ bean >
< property name ="maxCount" value ="10">
</ bean >
简化为
<
customerDAO
class
="org.springside....CustomerDAO"
maxCount
="10"/>
2、5.尽量使用ApplicationContext装配bean,而不是用import
像
Ant
脚本中
imports
一样,
Spring
的
import
元素对于模块化
bean
的装配非常有用,例如:
字
串
3
<beans>
<import resource="***.xml"/>
<import resource="***.xml"/>
<bean id="orderService"
class="com.lizjason.spring.OrderService"/>
</beans>
<import resource="***.xml"/>
<import resource="***.xml"/>
<bean id="orderService"
class="com.lizjason.spring.OrderService"/>
</beans>
然而,比起在XML中用imports预装配这些bean,利用ApplicationContext来配置它们将更加灵活,也可以使XML配置更加的易于管理。你可以像下面这样传递一个bean定义数组到ApplicationContext的构造函数中:
String[] serviceResources = {"***.xml","***.xml","***.xml"};
ApplicationContext orderServiceContext = new ClassPathXmlApplicationContext(serviceResources);
3
、
6.setter注入和构造函数注入,优先使用前者
Spring 提供了三种注入方式:构造函数注入, setter 注入和方法注入。一般我们使用前两种。
Spring 提供了三种注入方式:构造函数注入, setter 注入和方法注入。一般我们使用前两种。
<bean id="orderService"
class="com.xxx.spring.OrderService">
<constructor-arg ref="orderDAO"/>
</bean>
<bean id="billingService"
class="com.xxx.spring.BillingService">
<property name="billingDAO"
ref="billingDAO">
</bean>
4
、
7.为每个配置文件加一个描述注释
在
XML
配置文件中最好使用有描述性的
id
和
name
,而不是成堆的注释。另外,加一个文件描述头将会非常有用, 这个描述可以概括文件中定义的
bean
。另一个选择,你可以在
description
元素中加入描述信息。例如:
<beans>
<description>
This file defines billing service
related beans and it depends on
baseServices.xml,which provides
service bean templates...
</description>
...
</beans>
用 description 元素的一个好处就是工具可以很容易的把描述信息从这个元素中提取出来。
<beans>
<description>
This file defines billing service
related beans and it depends on
baseServices.xml,which provides
service bean templates...
</description>
...
</beans>
用 description 元素的一个好处就是工具可以很容易的把描述信息从这个元素中提取出来。
5
、
在
6
、9.如可能,尽量复用
bean定义(继承)
Spring
提供了一种类似于继承的机制来降低配置信息的重复并使
XML
配置更加的简单。一个子
bean
可以从它的父
bean
继承配置信息,本质上这个父
bean
就像它的子
bean
的一个模板。这是一个在大型项目中必须使用的特性。所有你要做的就是把父
bean
的
abstract
属性置为
true
,并在子
bean
中加以引用。例如:
<bean id="abstractService" abstract="true"
class="com.lizjason.spring.AbstractService">
<property name="companyName"
value="lizjason"/>
</bean>
<bean id="shippingService"
parent="abstractService"
class="com.lizjason.spring.ShippingService">
<property name="shippedBy" value="lizjason"/>
</bean>
shippingService bean 继承了 abstractService bean 的属性 companyName 的值 lizjason 。注意,如果你为 bean 声名一个 class 或工厂方法,这个 bean 将会默认为 abstract
<bean id="abstractService" abstract="true"
class="com.lizjason.spring.AbstractService">
<property name="companyName"
value="lizjason"/>
</bean>
<bean id="shippingService"
parent="abstractService"
class="com.lizjason.spring.ShippingService">
<property name="shippedBy" value="lizjason"/>
</bean>
shippingService bean 继承了 abstractService bean 的属性 companyName 的值 lizjason 。注意,如果你为 bean 声名一个 class 或工厂方法,这个 bean 将会默认为 abstract