Spirng AOP与事务配置记录

以下内容并不包括最新注解式的配置方法,为spring4.0,3.0,2.0适用的xml配置模式

Spring AOP与Transaction常见设置

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

<tx:advice id="txAdvice" transaction-manager="txDs">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" timeout="10000" read-only="false"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="daopointcut" expression="execution(* app.aresxu.springweb.transaction.DaoSupportInterface.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daopointcut"/>
</aop:config>

顺序为 bean=》tx:advice=》aop,

transaction基础类为 PlatformTransactionManager

也可以使用spring早期提供的TransationProxyFactoryBean实现事务切面

 <bean name="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
    <property name="transactionManager" ref="dataSourceTx"/>
   <property name="transactionAttributes">
     <props>
       <prop key="insert*">PROPAGATION_REQUIRED</prop>
       <prop key="update*">PROPAGATION_REQUIRED</prop>
       <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
     </props>
   </property>
 </bean> 

 <bean name="sqlmapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation">
            <value>classpath:ibatis/IbatisBean_config.xml</value>
    </property>
    <property name="dataSource">
            <ref bean="dataSource"/>
    </property>
 </bean>

<bean name="internal_ibatisdao" class="com.web.dao.IbatisDao">
    <property name="sqlmap" ref="sqlmapClient"></property>
</bean>

<bean name="ibatis_dao" parent="transactionProxy">
    <property name="target" ref="internal_ibatisdao"/>
</bean>

也继承时可以修改方法事务等级:

<bean id="mySpecialService" parent="txProxyTemplate">
    <property name="target">
        <bean class="org.springframework.samples.MySpecialServiceImpl">
        </bean>
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="store*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

事务的PROPAGATION在org.springframework.transaction.TransactionDefinition 中定义

有关参数方面

aop:pointcut expression如下

  • the execution of any public method:
execution(public * *(..))
  • the execution of any method with a name beginning with "set":
execution(* set*(..))
  • the execution of any method defined by the AccountService interface:
execution(* com.xyz.service.AccountService.*(..))
  • the execution of any method defined in the service package:
execution(* com.xyz.service..(..))
  • the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service...(..))

例如有:

execution(* x.y.service.*Service.get(String,int)) and args(name, age) 等形式

tx:method参数配置

Table 11.1. <tx:method/> settings

Attribute Required? Default Description

name


Yes

 
Method name(s) with which the transaction attributes are to be associated. The wildcard (*) character can be used to associate the same transaction attribute settings with a number of methods; for example, get*handle*on*Event,
and so forth.


propagation


No


REQUIRED


Transaction propagation behavior.


isolation


No


DEFAULT


Transaction isolation level.


timeout


No


-1


Transaction timeout value (in seconds).


read-only


No


false


Is this transaction read-only?


rollback-for


No

 
Exception(s) that trigger rollback; comma-delimited. For example, com.foo.MyBusinessException,ServletException.


no-rollback-for


No

 
Exception(s) that do not trigger rollback; comma-delimited. For example,com.foo.MyBusinessException,ServletException.

传统AOP配置有

<!-- this is the object that will be proxied by Spring‘s AOP infrastructure -->
    <bean id="fooService" class="x.y.service.DefaultFooService"/>

    <!-- this is the actual advice itself -->
    <bean id="profiler" class="x.y.SimpleProfiler"/>

    <aop:config>
        <aop:aspect ref="profiler">

            <aop:pointcut id="theExecutionOfSomeFooServiceMethod"
                expression="execution(* x.y.service.FooService.getFoo(String,int))
                and args(name, age)"/>

            <aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod"
                method="profile"/>

        </aop:aspect>
    </aop:config>
package x.y;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

public class SimpleProfiler {

    public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
        StopWatch clock = new StopWatch("Profiling for " + name + " and " + age + "");
        try {
            clock.start(call.toShortString());
            return call.proceed();
        } finally {
            clock.stop();
            System.out.println(clock.prettyPrint());
        }
    }
}

根据需要有before式,after式,around式及其他切入方法,这里是around

也可以使用早期spring提供的ProxyFactoryBean实现AOP

<bean id="personTarget" class="com.mycompany.PersonImpl">
    <property name="name" value="Tony"/>
    <property name="age" value="51"/>
</bean>

<bean id="myAdvisor" class="com.mycompany.MyAdvisor">
    <property name="someProperty" value="Custom string property value"/>
</bean>

<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor">
</bean>

<bean id="person"
    class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces" value="com.mycompany.Person"/>

    <property name="target" ref="personTarget"/>
    <property name="interceptorNames">
        <list>
            <value>myAdvisor</value>
            <value>debugInterceptor</value>
        </list>
    </property>
</bean>

使用这一类的ProxyAop 需要切面类继承一系列类(根据需要使用)

Advice是Advice型切面的超级接口,派生的子接口都有advice切面功能。

MethodInterceptor
BeforeAdvice 
ThrowsAdvice 
AfterReturningAdvice 
IntroductionInfo 
Advisor

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-28 15:56:04

Spirng AOP与事务配置记录的相关文章

Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪

【转】 Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分

spring 中常用的两种事务配置方式以及事务的传播性、隔离级别

转载:http://blog.csdn.net/qh_java/article/details/51811533 一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 [html] view plain copy <!-- 定义事务管理器 --> <bean id="transactionManager" class="

spring 实现事务配置的方式

spring 中常用的两种事务配置方式以及事务的传播性.隔离级别 一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 <!-- 定义事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSource

spring声明式事务配置详解

spring声明式事务配置详解 君子不器 2013年06月16日 编程世界 5273次阅读 查看评论 理解Spring的声明式事务管理实现 本节的目的是消除与使用声明式事务管理有关的神秘性.简单点儿总是好的,这份参考文档只是告诉你给你的类加上@Transactional注解,在配置文件中添加('<tx:annotation-driven/>')行,然后期望你理解整个过程是怎么工作的.此节讲述Spring的声明式事务管理内部的工作机制,以帮助你在面对事务相关的问题时不至于误入迷途,回朔到上游平静

Spring事务配置的五种方式

Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分

(转)解释一下SQLSERVER事务日志记录

本文转载自桦仔的博客http://www.cnblogs.com/lyhabc/archive/2013/07/16/3194220.html 解释一下SQLSERVER事务日志记录 大家知道在完整恢复模式下,SQLSERVER会记录每个事务所做的操作,这些记录会存储在事务日志里,有些软件会利用事务日志来读取 操作记录恢复数据,例如:log explorer 那么事务日志记录怎麽查看,里面都记录了些什么? 打开可以利用下面SQL语句来查看所在数据库的事务日志记录 1 USE [GPOSDB] -

Spring声明式事务配置的两种策略SpringAop和Bean后处理器的代理BeanNameAutoProxyCreator

Spring的事务配置有两种:1编程式事务管理配置:2声明式事务管理配置.下面介绍两种声明式事务的配置,声明式事务相比于编程式事务代码耦合更低,无序书写任何事务管理的先关代码.两种声明式事务配置策略分别是:SpringAop事务管理和Bean后处理器的代理BeanNameAutoProxyCreator管理事务. 1.SpringAop事务管理配置 1.1.配置数据源: <bean id="pycDataSource" class="com.mchange.v2.c3p

spring事务配置的坑

基于 <tx> 命名空间的声明式事务管理 前面两种声明式事务配置方式奠定了 Spring 声明式事务管理的基石.在此基础上,Spring 2.x 引入了 <tx> 命名空间,结合使用 <aop> 命名空间,带给开发人员配置声明式事务的全新体验,配置变得更加简单和灵活.另外,得益于 <aop> 命名空间的切点表达式支持,声明式事务也变得更加强大. 1.如果没有配置事务,数据库操作是无法成功的. 2. 如果用try catch里的操作是无法用事务管理的? Spr