1. 避免使用autowire:
It sacrifices the explicitness and maintainability of the configurations.
2.遵循命名规范
Using clear, descriptive, and consistent name conventions across the project is very helpful for developers to understand the XML configurations. For bean ID, for example, you can follow the Java class field name convention. The bean ID for an instance of OrderServiceDAO
would be orderServiceDAO
.For large projects, you can add the package name as the prefix of the bean ID.
3.采用简洁的结构
For example, the following:
<bean id="orderService"
class="com.lizjason.spring.OrderService">
<property name="companyName">
<value>lizjason</value>
</property>
<constructor-arg>
<ref bean="orderDAO">
</constructor-arg>
</bean>
can be rewritten in the shortcut form as:
<bean id="orderService"
class="com.lizjason.spring.OrderService">
<property name="companyName"
value="lizjason"/>
<constructor-arg ref="orderDAO"/>
</bean>
Note that there is no shortcut form for <ref local="...">
.4.使用类型而不是序号来指定构造函数的参数
For example, instead of:
<bean id="billingService"
class="com.lizjason.spring.BillingService">
<constructor-arg index="0" value="lizjason"/>
<constructor-arg index="1" value="100"/>
</bean>
It is better to use the type
attribute like this: <bean id="billingService"
class="com.lizjason.spring.BillingService">
<constructor-arg type="java.lang.String"
value="lizjason"/>
<constructor-arg type="int" value="100"/>
</bean>
5.重用bean定义
For example:
<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>
Note that if you do not specify a class or factory method for a bean definition, the bean is implicitly abstract.6.通过
ApplicationContext管理定义文件而不是用import.
Spring import
elements are useful for assembling modularized bean definitions. For example: <beans>
<import resource="billingServices.xml"/>
<import resource="shippingServices.xml"/>
<bean id="orderService"
class="com.lizjason.spring.OrderService"/>
<beans>
instead of pre-assembling them in the XML configurations using imports, it is more flexible to configure them through the ApplicationContext
.You can pass an array of bean definitions to the
ApplicationContext
constructor as follows: String[] serviceResources =
{"orderServices.xml",
"billingServices.xml",
"shippingServices.xml"};
ApplicationContext orderServiceContext = new
ClassPathXmlApplicationContext(serviceResources);
7.使用id作为bean的标识
8.dependency-check 的使用
9.为每个配置文件添加注释
you can add descriptions to the
description
element. For example: <beans>
<description>
This file defines billing service
related beans and it depends on
baseServices.xml,which provides
service bean templates...
</description>
...
</beans>
10.与开发组成员的沟通11.使用setter注入而不是构造函数注入
12.避免滥用依赖注入
Remember, with powerful IDEs, such as Eclipse and IntelliJ, Java code is much easier to read, maintain, and manage than XML files!
>>>> 原文地址