spring 事务通知

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.core.Ordered;
import org.springframework.util.StopWatch;

public class ExceptionDeal implements Ordered,MethodInterceptor{
  
    private int order;
    private Log  log = LogFactory.getLog(ExceptionDeal.class);
   
    public int getOrder() {
        // TODO Auto-generated method stub
        return this.order;
    }
    public void setOrder(int order) {

        this.order = order;

        }

    // this method is the around advice
    public Object profile(ProceedingJoinPoint call) throws Throwable {

        Object returnValue;
   
        StopWatch clock = new StopWatch(getClass().getName());
   
        try {
   
        clock.start(call.toShortString());
        try{
           
           returnValue = call.proceed();
          
        }catch(Exception e){
            e.printStackTrace();
            throw new Exception();
        }
   
        } finally {
   
        clock.stop();
   
        log.info(clock.prettyPrint());
   
        }

    return returnValue;

    }
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // TODO Auto-generated method stub
        log.info("Before: invocation=[" + invocation + "]");
       
        Object rval = invocation.proceed();
       
        log.info("Invocation returned");
       
        return rval;
    }

}

<!-- 事务通知类 -->
    <bean id="profiler"
        class="ExceptionDealClass">
        <property name="order" value="2" />
    </bean>

 <aop:aspect id="profilingAspect" ref="profiler">
            <!-- 通知类型为after-throwing-->
            <aop:around method="profile"
                  pointcut-ref="productServiceMethods"/>
    </aop:aspect>

猜你喜欢

转载自peacherdiy.iteye.com/blog/2059447
今日推荐