spring aspectj采用注释做申明式事务(手工山寨版) -凯发k8国际

`
qepwqnp
  • 浏览: 102716 次
  • 性别:
  • 来自: 成都
最近访客
博主相关
  • 博客
  • 微博
  • 相册
  • 收藏
  • 社区版块
    • ( 0)
    • ( 65)
    • ( 47)
    存档分类
    最新评论

    spring aspectj采用注释做申明式事务(手工山寨版)

    建立表

    drop table t_book;

     

    create table t_book(

    book_id int,

    book_name varchar2(20),

    book_author varchar2(10)

    );

    1:定义注释

    import java.lang.annotation.target;

    import java.lang.annotation.retention;

    importstatic java.lang.annotation.retentionpolicy.runtime;

    import java.lang.annotation.elementtype;

     

    @target({elementtype.method})

    @retention(runtime)

    public@interfacetrans {

        int myvalue();

    }

    2spring配置文件

    //设置为自动代理

    //设置代理类

    <bean id="transactiontemplate" class="org.springframework.transaction.support.transactiontemplate">

       <property name="transactionmanager">

        <ref bean="transactionmanager"/>

       property>

     bean>

     <bean id="transactionmanager"

           class="org.springframework.jdbc.datasource.datasourcetransactionmanager">

           <property name="datasource">

             <ref local="datasource"/>

           property>

           bean>

     <bean id="datasource"

           class="org.springframework.jdbc.datasource.drivermanagerdatasource">

           <property name="driverclassname">

             <value>oracle.jdbc.driver.oracledrivervalue>

           property>

           <property name="url">

             <value>jdbc:oracle:thin:@localhost:1521:orclvalue>

           property>

           <property name="username"><value>scottvalue>property>

           <property name="password"><value>tigervalue>property>

     bean>

     <bean id="bookservice" class="com.cl.annotation.bookservice">

       <property name="datasource">

         <ref bean="datasource"/>

       property>

       <property name="trans">

         <ref bean="transactiontemplate"/>

       property>

       bean>

    3:业务类

    publicclass bookservice {

        private transactiontemplate trans;

        private datasource datasource;

     

        public transactiontemplate gettrans() {

            returntrans;

        }

     

        publicvoid settrans(transactiontemplate trans) {

            this.trans = trans;

        }

     

        public datasource getdatasource() {

            returndatasource;

        }

     

        publicvoid setdatasource(datasource datasource) {

            this.datasource = datasource;

        }

        @trans(myvalue=transactiontemplate.propagation_required)//利用注释设置申明事务值

        publicvoid createbook(){

        jdbctemplate jdbc = new jdbctemplate(datasource);

        jdbc.update("insert into t_book values('1','spring3 in action','xxx')");

       

        }

    }

    4:代理类

     

    @aspect

    publicclass transactionaspect {

        @around("execution(@com.cl.annotation.trans * *.*(..))")//设置aop切入条件为在com.cl.annotation.

        public object intransaction(final proceedingjoinpoint pjp) {

            transactiontemplate trans = (transactiontemplate)springutil.getcontext().getbean("transactiontemplate");

       

            methodsignature sig = (methodsignature) pjp.getsignature();

            method method = sig.getmethod();

            trans antrans = (trans) method.getannotation(trans.class);

           

            trans.setpropagationbehavior(antrans.myvalue());//设置事务处理级别

           

            return trans.execute(new transactioncallback(){

                public object dointransaction(transactionstatus ts) {

                    object result = null;

                    try {

                        system.out.println("切入之前");

                        result=pjp.proceed();

                        system.out.println("切入之后");

                    } catch (throwable e) {

                        ts.setrollbackonly();

                        e.printstacktrace();

                    }

                    return result;

                }

           

            });

           

     

        }

    }

    分享到:
    评论

    相关推荐

      spring aspectjweaver-1.7.1.jar aspectj-1.8.9.jar aopalliance.jar

      以公司订单管理的模型为例(大部分步骤和业务了逻辑已经删除,只保存aop注解这一部分)spring aop

      spring通过aspectj来实现事务控制

      null 博文链接:https://tuoxinquyu.iteye.com/blog/1465187

      null 博文链接:https://tuoxinquyu.iteye.com/blog/1465155

      凯发k8国际娱乐官网入口官网最新 aspectjweaver.jar aspectjrt.jar aspectj-1.8.9-src.jar

      本书以aop基础理论为主线,首先讲解aop的产生与发展、为什么要应用aop、aop的核心概念,然后详细讲解aspectwerkz、aspectj、spring框架的aop应用开发技术。 随书附赠的光盘内容为本书开发的案例程序包。本书内容循序...

      spring aspectj aspectrt.jar

      本书以aop基础理论为主线,首先讲解aop的产生与发展、为什么要应用aop、aop的核心概念,然后详细讲解aspectwerkz、aspectj、spring框架的aop应用开发技术。 随书附赠的光盘内容为本书开发的案例程序包。本书内容循序...

      1: 分析解释静态代理框架 aspectj演变过程 2:增加aspectj案例 3:分析aspectj字节码

      spring 使用aspectj 实现 aop之前置通知小例子,实际跑过,验证可信。

      用户注册和登录是每个系统的必须存在的一部分,基于dwr struts2 spring hibernate写了一个用户登录注册系统。  其中用dwr去进行用户注册的用户是否存在的验证。  全部业务控制交由spring去进行处理。事务处理也...

      spring aop的aspectj支持jar包; 包括: com.springsource.net.sf.cglib-2.2.0.jar com.srpingsource.org.aopalliance-1.0.0.jar com.srpingsource.org.aspectj.weaver-1.68.release.jar

      spring 使用aspectj 实现 aop(基于xml文件、基于注解)

      spring和aspectj的aop实验,详细内容可以移步至博客:https://mp.csdn.net/postedit/97750888

      spring将事务管理分成了两类: * 编程式事务管理 * 手动编写代码进行事务管理.(很少使用) * 声明式事务管理: * 基于transactionproxyfactorybean的方式.(很少使用) * 需要为每个进行事务管理的类,配置一个...

      android realm数据库配合aop框架封装

       第10章:对实际应用中spring事务管理各种疑难问题进行透彻的剖析,让读者对spring事务管理不再有云遮雾罩的感觉。  第11章:讲解了如何使用spring jdbc进行数据访问操作,我们还重点讲述了lob字段处理、主键产生...

      1. 基于aspectj实现动态数据源...6. 实现事务内切换数据源(支持原生spring声明式事务哟,仅此一家),并支持多数据源事务回滚(有了它除了跨服务的事务你需要考虑分布式事务,其他都不需要,极大的减少了系统的复杂程度)

      spring spectj aop 前置通知 后置通知 返回通知 异常通知 环绕通知

    global site tag (gtag.js) - google analytics
    网站地图