• SpringBoot AOP示例


    AOP主要注解:

    @Aspect,作用在类上,说明这是一个Aspect切面类。
    @Pointcut,用来描述,你需要在哪些类的哪些方法中植入你的代码。
    @Adive,与Pointcut配合使用,主要说明在Pointcut标记方法的什么时机执行,执行之前?执行之后?

    @Pointcut express(切面表达式)

    designators指示器:
    匹配方法:execution() 通过什么方式去匹配哪些类的哪些方法(重点掌握)。
    匹配注解:@annotation() @args() @within() @target()
    匹配包/类型:within()
    匹配对象:this() bean() target()
    匹配参数:args()

    wildcards通配符:

     *  匹配任意数量的字符
     ..  匹配指定类及其子类
     +  匹配任意数的子包或参数
    

    operators运算符:&&与 ||或 !非

    5中Advice

    @Before
    @After
    @AfterReturning
    @AfterThrowing
    @Around

    示例代码

    @Aspect
    @Component
    public class HttpAspect {
        private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
    
        @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
        public void log(){
        }
    
        /**
         * 在截取的方法前执行
         * @param joinPoint
         */
        @Before("log()")
        public void doBefore(JoinPoint joinPoint){
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
    
            // url
            logger.info("url={}", request.getRequestURL());
            // method
            logger.info("method={}", request.getMethod());
            // ip
            logger.info("ip={}", request.getRemoteAddr());
            // 类方法
            logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            // 参数
            logger.info("args={}", joinPoint.getArgs());
        }
    
        /**
         * 获取方法返回参数
         * @param object
         */
        @AfterReturning(returning = "object", pointcut = "log()")
        public void doAfterReturning(Object object){
            logger.info("response={}", object);
        }
    }
    

    Advice示例

    @Aspect
    @Component
    public class AdviceAspectConfig {
    
        /********PointCut********/
    
        @Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
        public void matchAnnotation(){}
    
        @Pointcut("execution(* com.vmware.AopExecutionDemo.service.*.find*(int))")
        public void matchIntArgs(){}
    
        @Pointcut("execution(public * com.vmware.AopExecutionDemo.service..*.*(..) throws java.lang.IllegalAccessException)")
        public void matchExecption(){}
    
        @Pointcut("execution(int com.vmware.AopExecutionDemo.service.*.*(..))")
        public void matchReturn(){}
    
        /********Advice********/
    
        /**
         * 获取方法输入的参数
         * @param productId
         */
        @Before("matchIntArgs() && args(productId)")
        public void before(int productId){
            System.out.println("### before ### productId = [" + productId + "]");
        }
    
        /**
         *  获取方法返回参数
         * @param result
         */
        @AfterReturning(value = "matchReturn()", returning = "result")
        public void after(int result){
            System.out.println("### after ###:" + result);
        }
    
        /**
         * Before + After + AfterReturning
         * @param joinPoint
         * @return
         * @throws Throwable
         */
        @Around("matchReturn()")
        public Object after(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("### before ###");
            Object result;
            result = joinPoint.proceed(joinPoint.getArgs());
            System.out.println("### after ###" + result);
            return result;
        }   
    }
    

    @annotation示例

    @Aspect
    @Component
    public class AnnotationAspectConfig {
    
        /**
         * 匹配带有 AdminOnlyMethod Annotation的方法
         */
        @Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
        public void matchAnnotation(){}
    
        @Before("matchAnnotation()")
        public void before(){
            System.out.println("###matchAnnotation && Before");
        }
    }
    
    @Aspect
    @Component
    public class AnnotationClassAspectConfig {
    
        /**
         * 匹配带有 AdminOnlyClass Annotation的类
         */
        @Pointcut("@within(com.vmware.AopExecutionDemo.annotation.AdminOnlyClass)")
        public void annotationClassCondition(){}
    
    
        @Before("annotationClassCondition()")
        public void before(){
            System.out.println("###annotationClass && before");
        }
    }
    
  • 相关阅读:
    linux命令总结
    在阿里云centos7.6上部署vue.js2.6前端应用
    MongoDb语法
    Echarts 地图绘制
    在阿里云Centos7.6中部署nginx1.16+uwsgi2.0.18+Django2.0.4
    django--- 支付宝退款
    响应式网站设计(Responsive Web design)
    django -- 推荐商品算法
    django -- 美多订单分表
    小程序基本配置
  • 原文地址:https://www.cnblogs.com/vincenshen/p/10427872.html
Copyright © 2020-2023  润新知