【转】spring文档配置属性

Spring

1.DTD

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd">

  

以上是Spring 2.0的标准DTD,相比之下不是很喜欢用schema xsd文件式的定义,一大堆太长了,只有真正要使用Spring定义的Schema如TX,AOP 时才使用Schema。

 

2.default-lazy-init

     Spring的lazy-init,可以使单元测试与集成测试时的速度大大加快,不过要留意一些类比如XFire导出WebService的定义,还有Spring MVC的xx-servlet.xml文件,都不能定为lay-init,否则会吃大亏。

 

 

3.PropertyOverrideConfigurer

   

不同于PropertyPlaceholderConfigurer 替换context文件中的变量,PropertyOverrideConfigurer是在ApplicationContext 初始化的最后,强行将某些Bean的某些属性,替换成它的properties文件里的值。

    比如生产环境的jdbc.properties里定义了jdbc连接为Oracle,并通过PlaceholderConfigurer设置到<bean id="dataSource"> 里,在测试时再载入下面的applicationContex-test.xml文件,就能透明的将配置更改为嵌入式数据库。

 

applicationContext-test.xml: 定义载入的properties。

扫描二维码关注公众号,回复: 300618 查看本文章

 

<bean id="testPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
        <property name="location" value="classpath:spring/test/jdbc.properties"/>
</bean>

 

 

spring/test/jdbc.properties: 将ApplicationContext 中id为dataSource的bean的url属性改为使用hsqldb。

 

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

 

4. Spring 2.0的schema简写

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

 完整的schema列表见参考手册附录A: http://www.redsaga.com/spring_ref/2.0/html/xsd-config.html

 另外,附录B还提供了自行开发schema的方式。手册里宣称,普通应用项目团队要开发schema有点麻烦,但呼吁各开源项目团队开发都各自的schema,共同简化配置文件。

 其中有一种可能是最容意使用的默认schema是p,推进<propertity>节点写法的进一步简化。

 

<bean>
  <property name="foo" value="foovalue">
  <property name="fooBean" ref="fooBean"/>
</bean>

<!-- 简写为 -->
<bean p:foo="foovalue" p:fooBean-ref="fooBean"/> 

 

5.default-merge

   从Spring 2.0M2开始,beans支持default-merge= "true" 的定义,子类不需要重新定义父类的List型属性中已定义过的内容。在声明式事务体系下,一般会定义一个baseTxService基类

 

<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*等。

   不过Spring2.0已采用新的AOP写法,此方法的重要性下降。

<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>

 6.IMPORT

     如何组织ApplicationContext文件,决定了声明式编程会不会差一步变成配置地狱。SpringSide建议:为了单元测试,ApplicationContext文件尽量放ClassPath 而不是WEB-INF 目录。尽量使用<Import> 帮助以模块为单元组织ApplicationContext文件。如根目录的 /applicationContext.xml 和 springmvc-servlet.xml,都只定义一些公共的东西,然后以如下方式include模块里的applicationContext:

 

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

 

7.IntrospectorCleanupListener

  避免Struts,Quartz的内存泄露导致ClassLoader不能加载。详细见Spring的API DOC文档

 

   <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

猜你喜欢

转载自gdfdfg-tech.iteye.com/blog/1956778
今日推荐