• 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指定切面,方便维护

  • 相关阅读:
    新闻发布项目——实体类(comment)
    新闻发布项目——实体类(comment)
    新闻发布项目——实体类(newsTb)
    新闻发布项目——实体类(newsTb)
    新闻发布项目——实体类(newsTb)
    新闻发布项目——实体类(User)
    新闻发布项目——实体类(User)
    新闻发布项目——实体类(User)
    2016经典微小说:《轮回》
    要么干,要么滚,千万别混
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8528139.html
Copyright © 2020-2023  润新知