AspectJ 方式来处理 Spring 的 @Transactional 注解式事务(转)

news/2024/7/6 0:21:31 标签: spring, aop, 工作, application, 网络应用, class
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

class="type_original" title="原创">用 AspectJ 方式来处理 Spring 的 @Transactional 注解式事务

关键字: 事务处理 aspectj class="tags" href="/tags/SPRING.html" title=spring>spring

class="blog_content">

为节省你的时间, 如果你你不清楚什么是Spring 、AspectJ、事务处理,本文就不适合你看。你可以路过就走。

 

class="tags" href="/tags/WangLuoYingYong.html" title=网络应用>网络应用中,我们几乎总是需要严密控制我们class="tags" href="/tags/SPRING.html" title=spring>spring应用中的数据库事务的启动和结束。为做到这一点,我们或多或少都已经通过AOP来做这些事情。但一般都是在XXService、或XXController中封装处理请求的方法。

Spring有内置注解支持,因而你可以简单地通过@Transactional 注解你的方法或类,并在配置文件中添加<tx:annotation-driven />,把相应方法封装于一个事务之中。这听起来好像很简单

 

但是,所有这些都是通过Spring 的代理对象封装并环绕增强原来的被注解@Transactional 的类来实现的,但这种做法只有当事务方法是public的、并且是被代理类外部调用的情况下才会正常class="tags" href="/tags/GongZuo.html" title=工作>工作(可以参看Spring 事务处理模型图就明白,否则代理对象自己调用自己就会绕过对它的环绕事务增强,其他切面增强也是一样)。这就很不爽了,意味着你不能在XXService或XXController内部串联处理一些具各自独立的事务,例如在XXController调用handleRequestInternal。解决的办法是使用全功能完整成熟的AspectJ织入。AspectJ织入方式同样支持@Transactional (其他自定义注解也行^_^),同时能被织入到所有方法中(不只是public),并且在内不外部都可以。

 

AspectJ有三种方式织入事务代码

a.编译时(CTW). 拥有所有需要的源代码

b.运行时(LTW).

c.字节码织入(BTW).没有织入目标的源代码(如只有jar)

 

这里我们使用CTW的方式

class="dp-highlighter">
class="bar">
class="tools"> Xml代码 复制代码
    class="dp-xml">
  1. class="tag"><class="tag-name">beans class="attribute">xmlns=class="attribute-value">"http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/schema/beans" class="attribute">xmlns:class="tags" href="/tags/AOP.html" title=aop>aop=class="attribute-value">"http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/schema/class="tags" href="/tags/AOP.html" title=aop>aop"class="tag">>  
  2.   
  3.     class="tag"><class="tag-name">class="tags" href="/tags/AOP.html" title=aop>aop:class="tags" href="/tags/SPRING.html" title=spring>spring-configuredclass="tag">>  
  4.   
  5.     class="tag"><class="tag-name">bean class="attribute">id=class="attribute-value">"annotationTransactionAspect" class="attribute">factory-method=class="attribute-value">"aspectOf"  class="attribute">class=class="attribute-value">"org.class="tags" href="/tags/SPRING.html" title=spring>springframework.transaction.aspectj.AnnotationTransactionAspect"class="tag">>  
  6.         class="tag"><class="tag-name">property class="attribute">name=class="attribute-value">"transactionManager" class="attribute">ref=class="attribute-value">"transactionManager"class="tag">>class="tag"></class="tag-name">propertyclass="tag">>  
  7.     class="tag"></class="tag-name">beanclass="tag">>  
  8.   
  9.     class="comments"><!-- the rest of your class="tags" href="/tags/APPLICATION.html" title=application>application here -->  
  10. class="tag"></class="tag-name">beansclass="tag">>  
class="xml" style="display: none;"><beans xmlns="http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/schema/beans" xmlns:class="tags" href="/tags/AOP.html" title=aop>aop="http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/schema/class="tags" href="/tags/AOP.html" title=aop>aop">

    <class="tags" href="/tags/AOP.html" title=aop>aop:class="tags" href="/tags/SPRING.html" title=spring>spring-configured>

    <bean id="annotationTransactionAspect" factory-method="aspectOf"  class="org.class="tags" href="/tags/SPRING.html" title=spring>springframework.transaction.aspectj.AnnotationTransactionAspect">
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>

    <!-- the rest of your class="tags" href="/tags/APPLICATION.html" title=application>application here -->
</beans>

 上面就是所有让Spring 使用Aspectj 方式@Transactional 注解事务的所有配置了。

 

下面我们开始把环绕事务切面的增强代码织入到注解了@Transactional  的XXService、或XXController类中(其他任何注解了@Transactional  也可以)。我们使用ant来构建织入。我们写一个ant任务来做这件事

class="dp-highlighter">
class="bar">
class="tools"> Xml代码 复制代码
    class="dp-xml">
  1. class="tag"><class="tag-name">target class="attribute">name=class="attribute-value">"compileAndWeave"class="tag">>  
  2.   
  3.     class="tag"><class="tag-name">path class="attribute">id=class="attribute-value">"web-src.compile.class.path"class="tag">>  
  4.         class="comments"><!-- 指定编译时所需要的任意库, 包括AspectJ所需要的jar -->  
  5.         class="tag"><class="tag-name">path class="attribute">refid=class="attribute-value">"external.libs.path" class="tag">/>  
  6.     class="tag"></class="tag-name">pathclass="tag">>  
  7.   
  8.     class="comments"><!-- 正常地编译你的源代码到这个目录中-->  
  9.     class="tag"><class="tag-name">javac class="attribute">srcdir=class="attribute-value">"src"  class="attribute">destdir=class="attribute-value">"build/classes-preweave"  class="attribute">classpathref=class="attribute-value">"web-src.compile.class.path" class="tag">/>  
  10.   
  11.     class="comments"><!-- 查找"iajc" task -->  
  12.     class="tag"><class="tag-name">taskdef class="attribute">resource=class="attribute-value">"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"  class="attribute">classpath=class="attribute-value">"path/to/aspectj/aspectjtools.jar"class="tag">/>  
  13.   
  14.     class="comments"><!-- weave the just compiled classes from classes-preweave into classes -->  
  15.     class="tag"><class="tag-name">iajc  class="attribute">inpath=class="attribute-value">"build/classes-preweave"  
  16.              class="attribute">destdir=class="attribute-value">"build/classes"                                          class="comments"><!--正常javac编译的*.class文件,也就是我们注解了@Transactional  的XXService、或XXController类所在位置-->  
  17.              class="attribute">aspectpath=class="attribute-value">"path/to/class="tags" href="/tags/SPRING.html" title=spring>spring/class="tags" href="/tags/SPRING.html" title=spring>spring-aspects.jar"   class="comments"><!--AspectJ切面类所在位置如:org.class="tags" href="/tags/SPRING.html" title=spring>springframework.transaction.aspectj.AnnotationTransactionAspect切面的位置-->  
  18.              class="attribute">classpathref=class="attribute-value">"web-src.compile.class.path"        
  19.             class="attribute">verbose=class="attribute-value">"true" class="tag">/>  
  20.   
  21. class="tag"></class="tag-name">targetclass="tag">>  
class="xml" style="display: none;"><target name="compileAndWeave">

    <path id="web-src.compile.class.path">
        <!-- 指定编译时所需要的任意库, 包括AspectJ所需要的jar -->
        <path refid="external.libs.path" />
    </path>

    <!-- 正常地编译你的源代码到这个目录中-->
    <javac srcdir="src"  destdir="build/classes-preweave"  classpathref="web-src.compile.class.path" />

    <!-- 查找"iajc" task -->
    <taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"  classpath="path/to/aspectj/aspectjtools.jar"/>

    <!-- weave the just compiled classes from classes-preweave into classes -->
    <iajc  inpath="build/classes-preweave"
             destdir="build/classes"                                          <!--正常javac编译的*.class文件,也就是我们注解了@Transactional  的XXService、或XXController类所在位置-->
             aspectpath="path/to/class="tags" href="/tags/SPRING.html" title=spring>spring/class="tags" href="/tags/SPRING.html" title=spring>spring-aspects.jar"   <!--AspectJ切面类所在位置如:org.class="tags" href="/tags/SPRING.html" title=spring>springframework.transaction.aspectj.AnnotationTransactionAspect切面的位置-->
             classpathref="web-src.compile.class.path"     
            verbose="true" />

</target>

  

 编译后把你的war发布到任何web容器中他就能class="tags" href="/tags/GongZuo.html" title=工作>工作了,所有注解了@Transactional  的方法(各种可见度)都能正常的处理事务,如果是类级@Transactional  注解,该类的就所有public方法都有事务。而且被注解类的内外都能调用,这样,你完全可以撇开class="tags" href="/tags/SPRING.html" title=spring>spring那麻烦的代理了,还补充一句,如果你使用了DWR做为你的ajax后台的话,服务层如果是JDK代理的话,将无法class="tags" href="/tags/GongZuo.html" title=工作>工作。只能使用Cglib方式的代理。还有很多情况,Spring 代理模式和其他一些框架配合class="tags" href="/tags/GongZuo.html" title=工作>工作的时候会有问题,全部使用AspectJ,撇开Spring 代理模式,你会觉得真的很free。 


http://www.niftyadmin.cn/n/1050065.html

相关文章

beanRefContext.xml

将spring配置打包写好书写beanRefContext.xmldemo<?xml version"1.0" encoding"UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans default…

C++ 的背影           --C++之父Bjarne Stroustrup印象

热爱C的朋友请不要误会&#xff0c;我并不是在暗示“C已经日薄西山”&#xff0c;或者任何类似的意思。从语义上来说&#xff0c;C作为一门编程语言&#xff0c;当然不会有什么背影。事实上&#xff0c;我想说的是一个人的背影。因此这个题目显得有点突兀&#xff0c;甚至哗众取…

DDoS入门

DDoS简介“拒绝服务&#xff08;Denial-Of-Service&#xff09;***就是消耗目标主机或者网络的资源&#xff0c;从而干扰或者瘫痪其为合法用户提供的服务。”国际权威机构“SecurityFAQ”给出的定义。DDOS则是利用多台计算机机&#xff0c;采用了分布式对单个或者多个目标同时发…

jquery库说明

jquery-1.3.2通常称作标准版 jquery-1.3.2-vsdoc是供VS 2008的智能提示所使用的&#xff0c;称作文档版&#xff0c; Visual Studio 2008初始并不支持JQuery框架的智能提示&#xff0c;需要2个补丁文件&#xff1a; Visual Studio 2008 SP1 补丁 VS90SP1-KB958502 补丁 …

prometheus-operator 详细总结(helm一键安装)

一、介绍prometheus-operator 二、查看配置rbac授权 三、helm安装prometheus-operator 四、配置监控k8s组件 五、granafa添加新数据源 六、监控mysql七、alertmanager配置 最后、卸载prometheus-operator 一、概述 The Prometheus resource 声明性地描述了Prometheus deploymen…

pom.xml之(二)

pom作为项目对象模型。通过xml表示maven项目&#xff0c;使用pom.xml来实现。主要描述了项目&#xff1a;包括配置文件&#xff1b;开发者需要遵循的规则&#xff0c;缺陷管理系统&#xff0c;组织和licenses&#xff0c;项目的url&#xff0c;项目的依赖性&#xff0c;以及其他…

win10 同步批处理禁用和启用网卡

echo off echo 正在启用超级管理员权限... %1 %2ver|find "5.">nul&&goto :stmshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :st","","runas",1)(window.close)&g…

Struts2+FlashFileUpload文件上传并生成缩略图和添加水印(转)

以前用过SWFUpload &#xff0c;功能确实很强大。javaeye上有人发布了这样一篇文章step-by-step多文件WEB批量上传(swfupload)的完美解决方案 有兴趣的朋友可以看一 下。Leeo觉得SWFUpload稍微不足的就是界面的定制&#xff0c;总感觉自己定制出来的界面有点难登大雅之堂。下面…