步骤五:struts+spring 集成

在 struts 的配置文件 struts-config.xml 中以插件的方式集成spring,同时配置全局的委托代理(可选)。
注意:plug-in 插件必须写在配置文件的最后一项。

struts-config.xml配置文件:

 
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE struts-config PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"   
  4.     "http://struts.apache.org/dtds/struts-config_1_2.dtd"> 
  5.  
  6. <struts-config> 
  7.   <data-sources /> 
  8.   <form-beans > 
  9.   </form-beans> 
  10.   <global-exceptions /> 
  11.   <global-forwards /> 
  12.   <action-mappings > 
  13.   </action-mappings> 
  14.  
  15.   <!-- 集成Spring --> 
  16.   <!-- 全局委托代理 --> 
  17.   <controller> 
  18.         <set-property value="org.springframework.web.struts.DelegatingRequestProcessor" property="processorClass"/> 
  19.   </controller> 
  20.   <!-- 注入spring插件 --> 
  21.   <plug-in 
  22.     className="org.springframework.web.struts.ContextLoaderPlugIn"> 
  23.         <set-property property="contextConfigLocation" 
  24.     value="/WEB-INF/applicationContext.xml"/> 
  25.   </plug-in> 
  26. </struts-config> 

步骤六:在Spring中配置事务管理—applicationContext.xml

 
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans 
  3.     xmlns="http://www.springframework.org/schema/beans" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.     xmlns:p="http://www.springframework.org/schema/p" 
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans   
  8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
  9.     <!-- 配置数据源  --> 
  10.      <bean id="dataSource"     
  11.         class="org.apache.commons.dbcp.BasicDataSource"     
  12.         destroy-method="close"> 
  13.         <!--JDBC驱动-->     
  14.         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />   
  15.         <!--数据库URL-->    
  16.         <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />    
  17.         <!--数据库用户名-->   
  18.         <property name="username" value="scott" />    
  19.         <!--数据库密码-->   
  20.         <property name="password" value="tiger" />     
  21.     </bean>     
  22.     <!-- 事务管理器 --> 
  23.     <bean id="transactionManager"     
  24.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
  25.         <property name="dataSource" ref="dataSource" />     
  26.     </bean>   
  27.     <!-- 事务代理基类 --> 
  28.     <bean id="txProxyTemplate" abstract="true" lazy-init="true" 
  29.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
  30.         <property name="transactionManager" ref="transactionManager"></property> 
  31.         <property name="transactionAttributes"> 
  32.             <props> 
  33.                 <prop key="do*">PROPAGATION_REQUIRED,-Exception</prop> 
  34.             </props> 
  35.         </property> 
  36.     </bean>   
  37. </beans> 

事务特性:

PROPAGATION_REQUIRED: 在当前的事务中进行,如果没有就建立一个新的事务

PROPAGATION_REQUIRES_NEW: 建立一个新的事务,如果现存一个事务就暂停它

PROPAGATION_MANDATORY: 方法必须在一个现存的事务中进行,否则丢出异常

PROPAGATION_NESTED: 在一个嵌入的事务中进行,如果不是,则同PROPAGATION_REQUIRED

PROPAGATION_NEVER: 指出不应在事务中进行,如果有就丢出异常

PROPAGATION_NOT_SUPPORTED: 指出不应在事务中进行,如果有就暂停现存的事务

PROPAGATION_SUPPORTS: 支持现在的事务,如果没有就以非事务的方式执行

事务应用:

 

 
  
  1. <bean id="EmpService" parent="txProxyTemplate"> 
  2.    <property name="target"> 
  3. <bean class="com.test.EmpServiceImpl"> 
  4.          <property name="empDao" ref="EmpDao"></property> 
  5.      </bean> 
  6. </property> 
  7. </bean> 

注意:

不应该调用一个数据源的 getConnection()方法和Connectionclose()方法,而必须使用Spring org.springframework.jdbc.datasource.DataSourceUtils

如下:

Connection conn = DataSourceUtils.getConnection(dataSource);
    

DataSourceUtils.releaseConnection(conn, dataSource);