当前位置:首页 > JavaServer Page

利用Guice的Aop实现切面事务管理

canca15年前 (2011-08-15)JavaServer Page567

类似于Spring的声明式事务管理,Guice中也可以很容易的实现,这里我拿Guice+Ibatis举个例子。

 

1. Guice要支持aop,首先我们引入aopalliance.jar。

 

2. 声明自己的事务annotation

 

Java代码  
  1. package com.yingxia.server.common;   
  2.   
  3. import java.lang.annotation.ElementType;   
  4. import java.lang.annotation.Retention;   
  5. import java.lang.annotation.RetentionPolicy;   
  6. import java.lang.annotation.Target;   
  7.   
  8. @Target(ElementType.METHOD)   
  9. @Retention(RetentionPolicy.RUNTIME)   
  10. public @interface Transaction {   
  11. }  

 

3. 声明切面事务拦截器TransactionInterceptor

 

Java代码  
  1. package com.yingxia.server.common;   
  2.   
  3. import org.aopalliance.intercept.MethodInterceptor;   
  4. import org.aopalliance.intercept.MethodInvocation;   
  5.   
  6. public class TransactionInterceptor implements MethodInterceptor {   
  7.   
  8.     @Override  
  9.     public Object invoke(MethodInvocation mi) throws Throwable {   
  10.         Object obj = null;   
  11.         try {   
  12.             BaseDao.sqlMapper.startTransaction();   
  13.             System.out.println("事务开始");   
  14.             obj = mi.proceed();   
  15.             BaseDao.sqlMapper.commitTransaction();   
  16.             System.out.println("事务提交");   
  17.         } finally {   
  18.             BaseDao.sqlMapper.endTransaction();   
  19.             System.out.println("事务结束");   
  20.         }   
  21.         return obj;   
  22.     }   
  23.   
  24. }   

4. 最后把拦截器绑定到Guice全局的Injector当中。这里请注意静态引入import static。bindInterceptor方法的前两个参数一个是用来过滤类,一个用来过滤方法。好了,这样你所有GuiceRemoteServiceServlet类的子类的带有Transaction标注的方法都将被纳入事务管理了。

 

Java代码
  1. private static final Injector injector = Guice.createInjector(new Module() {   
  2.   
  3.     @Override  
  4.     public void configure(Binder binder) {   
  5.            
  6.         binder.bindInterceptor(   
  7.             subclassesOf(GuiceRemoteServiceServlet.class),    
  8.             any(),   
  9.             new ExceptionInterceptor()   
  10.         );   
  11.            
  12.         binder.bindInterceptor(   
  13.             subclassesOf(GuiceRemoteServiceServlet.class),    
  14.             annotatedWith(Transaction.class),   
  15.             new TransactionInterceptor()   
  16.         );   
  17.     }   
  18. });  

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/219.html

标签: JavaServer Page
分享给朋友:

“利用Guice的Aop实现切面事务管理” 的相关文章

JBoss,Tomcat 中文URL支持方法

JBOSS 找到jboss4的deploy\jbossweb-tomcat50.sar\server.xml,编辑该文件,在下面的XML节点中增加红色的字<Connector port="8080" address="${jboss.bind.address}"  &nbs…

在web.xml不认<taglib>解决办法

在web.xml不认<taglib>解决办法: 如果是头是这样的<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application&n…

<html:select>设置默认值

在ActionFrom的reset方法里赋初值就可以,这样不管是调用的action或jsp都会先调用reset方法。…

JSP动态include与静态include的区别

动态INCLUDE   jsp:include page="included.jsp" flush="true" />它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。静态INCLUDE   用include伪码实现,定不会检…

浏览网页时的错误代号

① 客户方错误   100  继续   101  交换协议  ② 成功   200  OK   201  已创建 &nbs…

FCKeditor的秘密

       哈哈。。由于项目的需要,这几天一直在搞FCKeditor。其实,FCKeditor配置很简单。但不知道怎么样。在我的项目里FCKeditor总不能在FireFox里显示。开始我还以为是我的配置有问题。但我从头到尾检查了配置…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。