论坛首页 Java企业应用论坛

[转]Spring 配置要点

浏览 3790 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2007-03-06  
原文:
http://wiki.javascud.org/display/springs/SpringConfig

1.DTD

xml 代码
  1. >

以上是Spring 2.0的标准DTD,相比之下不是很喜欢用schema xsd文件式的定义,一大堆太长了。

2.default-lazy-init

Spring的lazy-init,可以使单元测试与集成测试时的速度大大加快,不过要留意一些 BeanPostProcessor和BeanFactoryPostProcessor的子类如 PropertyPlaceHolderConfigurer,还有spring mvc的servlet.xml,都不能定为lay-init,否则会吃大亏。

3.PropertyOverrideConfigurer

不同于PropertyPlaceholderConfigurer,PropertyOverrideConfigurer主要为了测试环境等进行透明的后期配置特殊更改,如

applicationContext-test.xml:

xml 代码
  1. <bean id="testPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
  2. <property name="location" value="classpath:context/test/jdbc.properties"/>
  3. <!---->bean>

test.properties:

dataSource.url=jdbc:hsqldb:res:default-db

即会在测试时将ApplicationContext 中id为dataSource的bean的url 改为嵌入式数据库。

4. Spring 2.0的schema简写

Spring 2.0开始推进了一系列的简写发,从僵硬的<bean class="xxxx.xxx.xxx" id="xx">,划为<aop:xxxx>这样的形式。</aop:xxxx></bean>

Spring 2.0所提供的各schema见spring参考手册的附录1,附录2还提供了自行开发schema的方式。手册里宣称,普通团队要开发schema有点麻烦,但呼吁各开源团队开发各自的schema.

5.default-merge

从Spring 2.0M2开始,beans支持default-merge= "true" 的定义,子类不需要重新定义父类的List型属性中已定义过的内容。

在声明式事务体系下,一般会定义一个baseTxService基类

xml 代码
  1. <bean id="baseTxService"
  2. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  3. abstract="true">
  4. <property name="transactionManager" ref="transactionManager"/>
  5. <property name="proxyTargetClass" value="true"/>
  6. <property name="transactionAttributes">
  7. <props>
  8. <prop key="get*">PROPAGATION_REQUIRED,readOnly<!---->prop>
  9. <prop key="find*">PROPAGATION_REQUIRED,readOnly<!---->prop>
  10. <prop key="save*">PROPAGATION_REQUIRED<!---->prop>
  11. <prop key="update*">PROPAGATION_REQUIRED<!---->prop>
  12. <prop key="remove*">PROPAGATION_REQUIRED<!---->prop>
  13. <!---->props>
  14. <!---->property>
  15. <!---->bean>

可以在beans统一定义default-merge= true,也可以每个bean定义,则子类的transactionAtttributes只须定义子类新增的部分,无须再定义get*,save*等。

xml 代码
  1. <beans default-merge="true">
  2. <bean id="orderManager" parent="baseTxService">
  3. <property name="target">
  4. <bean class="org.springside.bookstore.service.OrderManager"/>
  5. <!---->property>
  6. <property name="transactionAttributes">
  7. <props>
  8. <prop key="shipOrder">PROPAGATION_REQUIRED<!---->prop>
  9. <!---->props>
  10. <!---->property>
  11. <!---->bean>
  12. <!---->beans>

6.IMPORT

如何组织ApplicationContext文件,决定了声明式编程会不会差一步变成配置地狱。SpringSide的建议,为了单元测试,ApplicationContext文件尽量放ClassPath 而不是WEB-INF 目录。另外,尽量使用<import> 帮助以模块为单元组织ApplicationContext文件。</import>

如根目录的 /applicationContext.xml 和 springmvc-servlet.xml,都只定义公共的一些东西,然后以如下方式include模块里的applicationContext:

xml 代码
  1. <import resource="classpath:org/springside/bookstore/admin/applicationContext-manager.xml"/>

7.ApplicationContext在WEB应用中的实例化

防止*配置地狱*的另一种方法,可以象下面那样使用ContextLoaderListener来在web.xml中注册一个ApplicationContext:
web.xml:

xml 代码
  1. <context-param>
  2. <param-name>contextConfigLocation<!---->param-name>
  3. <param-value>classpath*:spring/*.xml<!---->param-value>
  4. <!---->context-param>
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener<!---->listener-class>
  7. <!---->listener>

监听器首先检查contextConfigLocation参数,如果它不存在,它将使用/WEB-INF/applicationContext.xml作为默认值。

   发表时间:2007-05-13  
写的不错
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics