Spring 配置最佳实践

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!

>>>> 原文地址
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACPI表示高级配置和电源管理接口(Advanced Configuration and Power Management Interface)。ACPI(Advanced Configuration Management)是由 惠普/INTEL/MICROSOFT/TOSHIBA提出的新型电源管理规范,意图是让系统而不是BIOS来全面控制电源管理,使系统更加省电。 其特点主要有:提供立刻开机功能,即开机后可立即恢复到上次关机时的状态,光驱、软驱和硬盘在未使用时会自动关掉电源,使用时再打开;支持在开电状态下既插即拔,随时更换功能。 ACPI主要支持三种节电方式,1、(suspend即挂起)显示屏自动断电;只是主机通电。这时敲任意键即可恢复原来状态。2、(save to ram 或suspend to ram 即挂起到内存)系统把当前信息储存在内存中,只有内存等几个关键部件通电,这时计算机处在高度节电状态,按任意键后,计算机从内存中读取信息很快恢复到原来状态。3、(save to disk或suspend to disk即挂起到硬盘)计算机自动关机,关机前将当前数据存储在硬盘上,用户下次按开关键开机时计算机将无须启动系统,直接从硬盘读取数据,恢复原来状态。   ACPI可实现以下功能:   1、用户可以使外设在指定时间开关;   2、使用笔记本电脑的用户可以指定计算机在低电压的情况下进入低功耗状态,以保证重要的应用程序运行;   3、操作系统可以在应用程序对时间要求不高的情况下降低时钟频率;   4、操作系统可以根据外设和主板的具体需求为它分配能源;   5、在无人使用计算机时可以使计算机进入休眠状态,但保证一些通信设备打开;   6、即插即用设备在插入时能够由ACPI来控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值