AOP 重点介绍切点写法

实验之前要先配置spring配置文件

    
    

加入pom文件

        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.aspectj
            aspectjrt
            1.6.12
        
        
            org.aspectj
            aspectjweaver
            1.6.12
        
        
            cglib
            cglib
            2.2
        

定义接口

public interface IToAdviceClass {
    public String aroundAdvice(String arg0,Integer arg1);
    public String beforeAdvice(String arg0,Integer arg1);
    public Integer afterAdvice(Integer arg0);
}

定义实现类

@Service
public class ToAdviceClassImpl implements IToAdviceClass{
    @Override
    public String aroundAdvice(String arg0,Integer arg1){
        System.out.println("aroundAdvice");
        return "aroundAdvice";
    }
    @Override
    public String beforeAdvice(String arg0,Integer arg1){
        System.out.println("beforeAdvice");
        return "beforeAdvice";
    }
    @Override
    public Integer afterAdvice(Integer arg0){
        System.out.println("afterAdvice");
        return 0;
    }
}

定义切面类

@Component
@Aspect
public class MyAspect {
    @Before("execution(* com.ruochen.aoptest..*.*(..))")
    public String beforeAspect(JoinPoint joinPoint){
         System.out.println("命中所有方法:前置增强");
         return "xxxx";
    }
    @After("execution(* com.ruochen.aoptest.IToAdviceClass.afterAdvice(..))")
    public void afterAspect(){
        System.out.println("后置增强");
    }
    @Around("execution(* com.ruochen.aoptest.IToAdviceClass.aroundAdvice(..))")
    public void aroundAspect(ProceedingJoinPoint joinPoint){
        System.out.println("环绕前置增强");
        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("环绕后置增强");
    }
}

注意点:

ProceedingJoinPoin类只有在环绕增强中才能引用,还有几个有用的增强

  //声明后置通知  
    @AfterReturning(pointcut = "pointCutMethod()", returning = "returnValue")
    public void doAfterReturning(JoinPoint point,Object returnValue) throws Exception{
   //声明例外通知  
    @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
    public void doAfterThrowing(JoinPoint point,Exception e) throws Exception{
   //声明最终通知  
    @After("pointCutMethod()")  
    public void doAfter(JoinPoint point) throws Exception{

Spring AOP 之 通知、连接点、切点、切面。

@Component
@Conditional(TransactionAfterCondition.class)
@Aspect
@Slf4j
public class TransactionAfterAopAspect implements Ordered {

    /**
     * 构造方法
     */
    public TransactionAfterAopAspect(){

    }

    /**
     * 事务提交切入点
     */
    @Pointcut("execution(* org.springframework.transaction.PlatformTransactionManager.commit(..))")
    private void pointCutMethodCommit() {
    }

    /**
     * 事务回滚切入点
     */
    @Pointcut("execution(* org.springframework.transaction.PlatformTransactionManager.rollback(..))")
    private void pointCutMethodRollback() {
    }


    /**
     * 事务提交后处理
     * @param point
     * @throws Throwable
     */
    @AfterReturning("pointCutMethodCommit()")
    public void processCommit(JoinPoint point) throws Throwable {
        DefaultTransactionStatus status = (DefaultTransactionStatus) point.getArgs()[0];
        if (status.isLocalRollbackOnly() || status.isGlobalRollbackOnly() || !status.isNewTransaction()||status.hasSavepoint()) {
            return;
        }
        LazyTaskHelper.executeByType(TransactionTaskType.COMMIT);
        LazyTaskHelper.executeByType(TransactionTaskType.ALLWAYS);
        LazyTaskHelper.removeByType(TransactionTaskType.ROLLBACK);
        log.info("AOP AfterReturning Commit");
    }

    /**
     * 是回滚处理
     * @param point
     * @throws Throwable
     */
    @After("pointCutMethodRollback()")
    public void processRollback(JoinPoint point) throws Throwable {
        LazyTaskHelper.executeByType(TransactionTaskType.ROLLBACK);
        LazyTaskHelper.executeByType(TransactionTaskType.ALLWAYS);
        LazyTaskHelper.removeByType(TransactionTaskType.COMMIT);
        log.info("AOP After Rollback");
    }

    @Override
    public int getOrder() {
        return 3000;
    }
}

你可能感兴趣的:(AOP 重点介绍切点写法)