spring 配置事务---配置文件方式

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 连接池 --> 
<!--   <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">  -->
<!--        <property name="jndiName">  -->
<!--            <value>java:jboss/datasources/ptDs</value>  -->
<!--        </property>  -->
<!--     </bean>   -->


    
<!--     <bean id="dataSourceMer" class="org.springframework.jndi.JndiObjectFactoryBean"> -->
<!--        <property name="jndiName"> -->
<!--            <value>java:jboss/datasources/merDs</value> -->
<!--        </property> -->
<!--     </bean> -->
    
 
  <!-- 数据库连接池 --> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="10"></property>
        <property name="minPoolSize" value="10"></property>
        <property name="initialPoolSize" value="10"></property>
        <property name="maxIdleTime" value="20"></property>
        <property name="acquireIncrement" value="5"></property>
        <property name="idleConnectionTestPeriod" value="60"></property>
        <property name="preferredTestQuery" value="select 1"></property>
</bean>


<!-- Mybatis's sqlSessionFactory config -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:config/mybatis-config.xml" />

<!-- 缓存设置 -->
<bean id="distributeCache" class="com.kortide.ksfulife.distributecache.RedisCache" />   


<!-- 启用注解式事务 -->
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />  -->



  <bean name="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
  </bean> 



<!-- 启用声明式事务 -->
<!-- <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />  -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="updateWelfare" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config expose-proxy="true">
        <!-- 只对业务逻辑层实施事务 -->
        <aop:pointcut id="txPointcut" expression="execution(*  com.kortide.serviceImpl.welfare..*.*(..))" />
        <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
    </aop:config>

</beans>


1.配置文件方式

其中主要配置中是tx:adviceaop:config两个配置节,以Spring AOP的方式实现事务管理。

tx:advice配置了事务的管理者是transactionManager,同时tx:method也规定了如果方法名匹配“add*”和“get*”方法时使用事务,propagation是设定事务的传播级别。除了updateWelfare方法,其他的方法的事务是只读的(典型地,对于只执行查询的事务你会将该属性设为true,如果出现了更新、插入或是删除语句时只读事务就会失败)

aop:config指定了一个aop:pointcut去引用上边的advice。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

8一天不

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值