• Spring之重用切入点表达式


    import java.util.Arrays;
    import java.util.List;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class Log {
        
        @Pointcut("execution(* com.atguigu.spring.aop.impl.Cacl.*(int,int))")
        public void declareJoinPoint(){}
    
        @Before("declareJoinPoint()")
        public void before(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            List<Object> args = Arrays.asList(joinPoint.getArgs());
            System.out.println("before method"+methodName+" begins with "+args);
        }
        
        //后置通知,无论是否出现异常
        @After("declareJoinPoint()")
        public void after(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("after method "+methodName+" ends");
        }
        
        @AfterReturning(value="declareJoinPoint()",returning="result")
        public void afterReturning(JoinPoint joinPoint,Object result){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("afterreturning method "+methodName+" ends with "+result);
        }
        
        @AfterThrowing(value="declareJoinPoint()",throwing="exception")
        public void afterThrowing(JoinPoint joinPoint,Exception exception){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("afterthrowing method "+methodName +"occurs by "+exception);
        }
        
        @Around("declareJoinPoint()")
        public Object around(ProceedingJoinPoint pjp){
            Object result = null;
            String methodName = pjp.getSignature().getName();
            
            try {
                System.out.println(" around method "+methodName +" begins with "+Arrays.asList(pjp.getArgs()));
                result = pjp.proceed();
                System.out.println("around method "+methodName +" ends ");
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return result;
        }    
    }
    import java.util.Arrays;
    import java.util.List;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class Log {
        
        @Pointcut("execution(* com.atguigu.spring.aop.impl.Cacl.*(int,int))")
        public void declareJoinPoint(){}
    
        @Before("declareJoinPoint()")
        public void before(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            List<Object> args = Arrays.asList(joinPoint.getArgs());
            System.out.println("before method"+methodName+" begins with "+args);
        }
        
        //后置通知,无论是否出现异常
        @After("declareJoinPoint()")
        public void after(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("after method "+methodName+" ends");
        }
        
        @AfterReturning(value="declareJoinPoint()",returning="result")
        public void afterReturning(JoinPoint joinPoint,Object result){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("afterreturning method "+methodName+" ends with "+result);
        }
        
        @AfterThrowing(value="declareJoinPoint()",throwing="exception")
        public void afterThrowing(JoinPoint joinPoint,Exception exception){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("afterthrowing method "+methodName +"occurs by "+exception);
        }
        
        @Around("declareJoinPoint()")
        public Object around(ProceedingJoinPoint pjp){
            Object result = null;
            String methodName = pjp.getSignature().getName();
            
            try {
                System.out.println(" around method "+methodName +" begins with "+Arrays.asList(pjp.getArgs()));
                result = pjp.proceed();
                System.out.println("around method "+methodName +" ends ");
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return result;
        }    
    }

    @Pointcut指定切面,方便维护

  • 相关阅读:
    【Todo】各种语言里面的for循环 & loop
    git/icode操作记录
    UVA 639 (13.08.25)
    TkinterGUI
    [置顶] think in java interview-高级开发人员面试宝典(七)
    通过memcached来实现对tomcat集群中Session的共享策略
    Hibernate 配置详解(7)
    python模块之bsddb: bdb高性能嵌入式数据库 1.基础知识
    Python基础
    Objective-C中NSArray和NSMutableArray的基本用法
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8528139.html
Copyright © 2020-2023  润新知