• Java--通过Spring AOP进行事务管理


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
       <!-- Initialization for data source -->
       <bean id="dataSource" 
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
          <property name="username" value="root"/>
          <property name="password" value="cohondob"/>
       </bean>
       <!-- Initialization for TransactionManager -->
       <bean id="transactionManager"
       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource"  ref="dataSource" />    
       </bean>
       <tx:advice id="txAdvice"  transaction-manager="transactionManager">
          <tx:attributes>
          <tx:method name="create"/>
          </tx:attributes>
       </tx:advice>
    
       <aop:config>
          <aop:pointcut id="createOperation" 
          expression="execution(* com.tutorialspoint.StudentJDBCTemplate.create(..))"/>
          <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation"/>
       </aop:config>
    
       <!-- Definition for studentJDBCTemplate bean -->
       <bean id="studentJDBCTemplate"  
       class="com.tutorialspoint.StudentJDBCTemplate">
          <property name="dataSource"  ref="dataSource" />  
       </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"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
                         http://www.springframework.org/schema/beans/spring-beans.xsd  
                         http://www.springframework.org/schema/tx  
                         http://www.springframework.org/schema/tx/spring-tx.xsd  
                         http://www.springframework.org/schema/aop  
                         http://www.springframework.org/schema/aop/spring-aop.xsd "  
    >  
      
    <bean id="transactionManager"  
            class="org.springframework.orm.hibernate3.HibernateTransactionManager"  
            abstract="false" lazy-init="default" autowire="default"  
            dependency-check="default">  
            <property name="sessionFactory">  
                <ref bean="sessionFactory" />  
            </property>  
        </bean>  
        <tx:advice id="txAdvice" transaction-manager="transactionManager">  
            <tx:attributes>  
                <tx:method name="add*" propagation="REQUIRED" />  
                <tx:method name="delete*" propagation="REQUIRED" />  
                <tx:method name="update*" propagation="REQUIRED" />  
                <tx:method name="add*" propagation="REQUIRED" />  
                <!-- <tx:method name="*" propagation="true" />-->  
            </tx:attributes>  
      
        </tx:advice>  
      
        <aop:config>  
            <aop:pointcut id="allManagerMethod"  
                expression="execution(* com.service.*.*(..))" />  
            <aop:advisor advice-ref="txAdvice"  
                pointcut-ref="allManagerMethod" />  
        </aop:config>  
    </beans>  

    我解释一下(* com.evan.crm.service.*.*(..))中几个通配符的含义: 

    |第一个 * —— 通配 任意返回值类型| 
    |第二个 * —— 通配 包com.evan.crm.service下的任意class| 
    |第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法| 
    |第四个 .. —— 通配 方法可以有0个或多个参数| 

    转自:http://baobao707.iteye.com/blog/415446

    http://wiki.jikexueyuan.com/project/spring/transaction-management/spring-declarative-transaction-management.html

  • 相关阅读:
    java8学习之Collector复合与注意事项
    动画学习之WIFI图形绘制
    java线程基础巩固---多线程死锁分析,案例介绍
    java8学习之Collector同一性与结合性分析
    java8学习之Collector源码分析与收集器核心
    java8学习之Stream分组与分区详解
    kotlin面向对象之抽象类、继承、多态
    matplotlib-曲线和折线案例
    人口、人口密度分析项目-条形图
    开机自启:bat实现一次性打开win7中的常用软件和文件夹
  • 原文地址:https://www.cnblogs.com/eoss/p/5912485.html
Copyright © 2020-2023  润新知