论坛首页 Java企业应用论坛

Spring+AspectJ采用注释做申明式事务(手工山寨版)

浏览 2334 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-07   最后修改:2009-06-07

建立表

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;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.ElementType;

 

@Target({ElementType.METHOD})

@Retention(RUNTIME)

public @interface Trans {

    int myvalue();

}

2Spring配置文件

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>//设置为自动代理

<bean id="transAspect" class="com.lovo.annotation.TransactionAspect"></bean>//设置代理类

<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.OracleDriver</value>

       </property>

       <property name="url">

         <value>jdbc:oracle:thin:@localhost:1521:orcl</value>

       </property>

       <property name="username"><value>scott</value></property>

       <property name="password"><value>tiger</value></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:业务类

public class BookService {

    private TransactionTemplate trans;

    private DataSource dataSource;

 

    public TransactionTemplate getTrans() {

        return trans;

    }

 

    public void setTrans(TransactionTemplate trans) {

        this.trans = trans;

    }

 

    public DataSource getDataSource() {

        return dataSource;

    }

 

    public void setDataSource(DataSource dataSource) {

        this.dataSource = dataSource;

    }

    @Trans(myvalue=TransactionTemplate.PROPAGATION_REQUIRED)//利用注释设置申明事务值

    public void createBook(){

    JdbcTemplate jdbc = new JdbcTemplate(dataSource);

    jdbc.update("insert into T_BOOK values('1','Spring3 in Action','xxx')");

   

    }

}

4:代理类

 

@Aspect

public class 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;

            }

       

        });

       

 

    }

}

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics