spring是一个强大的java应用框架,广泛地应用于java的应用程序。为plain old java objects(pojos)提供企业级服务。spring利用依赖注入机制来简化工作,同时提高易测性。spring beans及依赖,以及beans类所需的服务都在配置文件中进行了详细的说明,这个配置文件是典型的xml格式。但是它既冗长又不实用。对于需要定义大量spring beans的大工程来说,我们难以阅读和管理它。
版权声明:任何获得matrix授权的网站,转载时请务必保留以下作者信息和链接
作者:jason;li;evenbetter(作者的blog:http://blog.matrix.org.cn/page/evenbetter)
原文:http://www.onjava.com/pub/a/onjava/2006/01/25/spring-xml-configuration-best-practices.html
译文:http://www.matrix.org.cn/resource/article/44/44236_spring+xml+configurations.html
关键字:spring;xml;configurations
在这篇文章里,对于spring xml的配置,我将向你展示12种比较好的实践。其中的一些实践不仅是好的实践,更是必要的实践。除此以外,还有其他因素,例如领域模型的设计,都能影响xml的配置,但是这篇文章重点研究xml配置的易读性和易管理性。
1。不要使用autowiring
spring可以通过类的自省来自动绑定其依赖部分,使得你不必明确指明bean的属性和构造器。bean的属性可以通过属性名称或类型匹配来实现自动绑定。构造器通过类型匹配来实现自动绑定。你甚至可以指定自动检测自动绑定模式,它可以引导spring选择一种适当的运行机制。先来看看下面的一个例子:
<bean id="orderservice"
class="com.lizjason.spring.orderservice"
autowire="byname"/>
<bean id="orderservice"
class="com.lizjason.spring.orderservice">
<property name="companyname">
<value>lizjason</value>
</property>
<constructor-arg>
<ref bean="orderdao">
</constructor-arg>
</bean>
<bean id="orderservice"
class="com.lizjason.spring.orderservice">
<property name="companyname"
value="lizjason"/>
<constructor-arg ref="orderdao"/>
</bean>
<bean id="billingservice"
class="com.lizjason.spring.billingservice">
<constructor-arg index="0" value="lizjason"/>
<constructor-arg index="1" value="100"/>
</bean>
<bean id="billingservice"
class="com.lizjason.spring.billingservice">
<constructor-arg type="java.lang.string"
value="lizjason"/>
<constructor-arg type="int" value="100"/>
</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>
<beans>
<import resource="billingservices.xml"/>
<import resource="shippingservices.xml"/>
<bean id="orderservice"
class="com.lizjason.spring.orderservice"/>
<beans>
string[] serviceresources =
{"orderservices.xml",
"billingservices.xml",
"shippingservices.xml"};
applicationcontext orderservicecontext = new
classpathxmlapplicationcontext(serviceresources);
<bean id="orderservice"
class="com.lizjason.spring.orderservice"
dependency-check="objects">
<property name="companyname"
value="lizjason"/>
<constructor-arg ref="orderdao"/>
</bean>
<beans>
<description>
this file defines billing service
related beans and it depends on
baseservices.xml,which provides
service bean templates...
</description>
...
</beans>
<bean id="orderservice"
class="com.lizjason.spring.orderservice">
<constructor-arg ref="orderdao"/>
</bean>
<bean id="billingservice"
class="com.lizjason.spring.billingservice">
<property name="billingdao"
ref="billingdao">
</bean>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器