[转]Spring 配置要点
原文:
http://wiki.javascud.org/display/springs/SpringConfig
1.DTD
Spring 2.0所提供的各schema见spring参考手册的附录1,附录2还提供了自行开发schema的方式。手册里宣称,普通团队要开发schema有点麻烦,但呼吁各开源团队开发各自的schema.
5.default-merge从Spring 2.0M2开始,beans支持default-merge= "true" 的定义,子类不需要重新定义父类的List型属性中已定义过的内容。
在声明式事务体系下,一般会定义一个baseTxService基类
xml 代码
- <bean id="baseTxService"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- abstract="true">
- <property name="transactionManager" ref="transactionManager"/>
- <property name="proxyTargetClass" value="true"/>
- <property name="transactionAttributes">
- <props>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly<!---->prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly<!---->prop>
- <prop key="save*">PROPAGATION_REQUIRED<!---->prop>
- <prop key="update*">PROPAGATION_REQUIRED<!---->prop>
- <prop key="remove*">PROPAGATION_REQUIRED<!---->prop>
- <!---->props>
- <!---->property>
- <!---->bean>
可以在beans统一定义default-merge= true,也可以每个bean定义,则子类的transactionAtttributes只须定义子类新增的部分,无须再定义get*,save*等。
xml 代码6.IMPORT
- <beans default-merge="true">
- <bean id="orderManager" parent="baseTxService">
- <property name="target">
- <bean class="org.springside.bookstore.service.OrderManager"/>
- <!---->property>
- <property name="transactionAttributes">
- <props>
- <prop key="shipOrder">PROPAGATION_REQUIRED<!---->prop>
- <!---->props>
- <!---->property>
- <!---->bean>
- <!---->beans>
如何组织ApplicationContext文件,决定了声明式编程会不会差一步变成配置地狱。SpringSide的建议,为了单元测试,ApplicationContext文件尽量放ClassPath 而不是WEB-INF 目录。另外,尽量使用<import> 帮助以模块为单元组织ApplicationContext文件。</import>
如根目录的 /applicationContext.xml 和 springmvc-servlet.xml,都只定义公共的一些东西,然后以如下方式include模块里的applicationContext:
xml 代码7.ApplicationContext在WEB应用中的实例化
- <import resource="classpath:org/springside/bookstore/admin/applicationContext-manager.xml"/>
防止*配置地狱*的另一种方法,可以象下面那样使用ContextLoaderListener来在web.xml中注册一个ApplicationContext:
web.xml:
xml 代码
- <context-param>
- <param-name>contextConfigLocation<!---->param-name>
- <param-value>classpath*:spring/*.xml<!---->param-value>
- <!---->context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener<!---->listener-class>
- <!---->listener>
监听器首先检查contextConfigLocation参数,如果它不存在,它将使用/WEB-INF/applicationContext.xml作为默认值。
1 楼 owenhappy 2007-03-30 ding 2 楼 javaway 2007-05-13 写的不错