首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

[转]Spring 配备要点

2012-10-24 
[转]Spring 配置要点原文:http://wiki.javascud.org/display/springs/SpringConfig1.DTDSpring 2.0所提供

[转]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 代码 
  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作为默认值。

1 楼 owenhappy 2007-03-30   ding 2 楼 javaway 2007-05-13   写的不错

热点排行