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

  • 相关阅读:
    HDU 1548 A strange lift (Dijkstra)
    HDU 1217 Arbitrage (Floyd)
    HDU 1385 Minimum Transport Cost (Dijstra 最短路)
    考研总结 2016-12-31 20:10 219人阅读 评论(21) 收藏
    归并排序 2016-12-30 20:17 208人阅读 评论(21) 收藏
    docker安装 2016-11-06 19:14 299人阅读 评论(31) 收藏
    Docker初步了解 2016-10-30 20:46 279人阅读 评论(31) 收藏
    [自考]感想 2016-10-23 20:28 261人阅读 评论(32) 收藏
    [自考]C++中一些特殊用法 2016-10-16 22:12 318人阅读 评论(30) 收藏
    Fitnesse批量读取变量信息,并保存到用例执行上下文中
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8528139.html
Copyright © 2011-2022 走看看