SSH的事物代理,applicationContext.xml的完整配置示例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 <!-- 建立数据源 -->
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <!-- 数据库驱动 -->
  <property name="driverClassName"
   value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
  </property>
  <!-- 数据库连接地址 -->
  <property name="url"
   value="jdbc:microsoft:sqlserver://localhost:1433">
  </property>
  <!-- 数据库的用户名和密码 -->
  <property name="username" value="sa"></property>
  <property name="password" value="sa"></property>
 </bean>

 <!-- 把数据源注入给SessionFactory  -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <!-- 配置数据库方言 -->
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <!-- 配置映射文件 -->
  <property name="mappingResources">
   <list>
    <value>com/test/pojo/TDepartment.hbm.xml</value>
    <value>com/test/pojo/TEmployee.hbm.xml</value>
   </list>
  </property>
 </bean>

 <!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <!-- 为事务管理器注入SessionFactory -->
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <!-- 配置事务代理,注入事务管理transactionManager,由Spring来代理事务,设置事务属性 -->
 <bean id="transactionProxy" abstract="true"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="remove*">
     PROPAGATION_REQUIRED,-Exception
    </prop>
    <prop key="delete*">
     PROPAGATION_REQUIRED,-Exception
    </prop>
    <prop key="update*">
     PROPAGATION_REQUIRED,-Exception
    </prop>
    <prop key="create*">
     PROPAGATION_REQUIRED,-Exception
    </prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

</beans>

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 <!-- DAO的注入配置与引用 -->
 <bean id="employeeDao" class="com.test.dao.Impl.EmpDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>

 <bean id="departmentDao" class="com.test.dao.Impl.DepDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>

 <!-- Service的注入配置与引用 -->
 <bean id="employTempService"
  class="com.test.service.Impl.TEmployeeServiceImpl">
  <property name="itemployee">
   <ref bean="employeeDao" />
  </property>
 </bean>

 <bean id="departTempService"
  class="com.test.service.Impl.TDepartmentServiceImpl">
  <property name="idepartment">
   <ref bean="departmentDao" />
  </property>
 </bean>

 <!-- 将Service注入给事务 -->
 <bean id="employService" parent="transactionProxy">
  <property name="target">
   <ref bean="employTempService" />
  </property>
 </bean>
 
 <bean id="departService" parent="transactionProxy">
  <property name="target">
   <ref bean="departTempService" />
  </property>
 </bean>

 <!-- Action的注入配置与引用 -->
 <bean name="/emp" class="com.test.action.EmpAction"
  abstract="false">
  <property name="employeeService">
   <ref bean="employService" />
  </property>
  <property name="departmentService">
   <ref bean="departService" />
  </property>
 </bean>

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值