zoukankan      html  css  js  c++  java
  • 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指定切面,方便维护

  • 相关阅读:
    Java中Date和Calender类的使用方法
    看《做性能测试需要做些什么》
    【笔记】jquery append,appendTo,prepend,prependTo 介绍
    【实践】jquery实现滑动动画及轮播
    【实践】四联联动 + 更加优化
    【实践】jQuery实现三联联动
    关于导入excel报错的处理(xls,xlsx)
    递归算法及经典案例
    验证身份证真假
    正则表达式
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8528139.html
Copyright © 2011-2022 走看看