• 六、SpringBoot整合aop(SpringBoot系列)


    SpringBoot整合aop

    @Component
    @Aspect //切面声明
    public class LogComponent {
        // 标注拦截的方法
        // 后面*的表示org.javaboy.aop.service目录下所有方法,方法入参为 数量任意,类型任意
        @Pointcut("execution(* org.javaboy.aop.service.*.*(..))")
        public void pc1() {
        }
    
        @Before(value = "pc1()") //前置通知
        public void before(JoinPoint jp) {
            // 方法名
            String name = jp.getSignature().getName();
            System.out.println("before--" + name);
        }
    
        @After(value = "pc1()") // 后置通知
        public void after(JoinPoint jp) {
            String name = jp.getSignature().getName();
            System.out.println("after--" + name);
        }
    
        // 返回通知 returning指返回值
        @AfterReturning(value = "pc1()", returning = "result") //返回通知
        public void afterReturning(JoinPoint jp, Object result) {
            String name = jp.getSignature().getName();
            System.out.println("afterReturning----" + name + "-----" + result);
        }
    
       
        @AfterThrowing(value = "pc1()",throwing = "e")  // 异常通知
        public void afterThrowing(JoinPoint jp,Exception e) {
            String name = jp.getSignature().getName();
            System.out.println("afterThrowing---"+name+"----"+e.getMessage());
        }
    
        // 环绕通知,相当于上面的四个的综合
        @Around("pc1()")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            // pjp就是被拦截的方法,调用proceed()方法,被拦截的方法才会被执行
            // 想用前置通知,就在调用proceed()方法前
            // 想用前置通知,就在调用proceed()方法后
            // 想用异常通知,就在try-catch 调用proceed()方法
            Object proceed = pjp.proceed();
            return "www.javaboy.org";
        }
    }
    

    转载:https://www.cnblogs.com/itsoku123/p/10744244.html

    文中有九种表达式的详细介绍

    @Pointcut中有中9种切入点表达式的写法

    1. execute
    2. within
    3. this
    4. target
    5. args
    6. @target
    7. @within
    8. @annotation
    9. @args

    1. execute

    拦截任意公共方法

    execution(public * *(..))
    

    拦截以set开头的任意方法

    execution(* set*(..))
    

    拦截类或者接口中的方法

    execution(* com.java.service.AccountService.*(..))
    

    拦截包中定义的方法,不包含子包中的方法

    execution(* com.java.service.*.*(..))
    

    拦截com.java.service包中所有类中任意方法,不包含子包中的类

    拦截包或者子包中定义的方法

    execution(* com.java.service..*.*(..))
    

    拦截com.java.service包或者子包中定义的所有方法

    2.within表达式

    表达式格式:包名.* 或者 包名..*

    拦截包中任意方法,不包含子包中的方法

    within(com.java.service.*)
    

    拦截service包中任意类的任意方法

    拦截包或者子包中定义的方法

    within(com.java.service..*)
    

    拦截service包及子包中任意类的任意方法

    3.@annotation表达式

    匹配有指定注解的方法(注解作用在方法上面)

    @annotation(com.java.demo2.Annotation)
    

    被调用的方法包含指定的注解的会被拦截

    应用场景

    常见用于记录日志、异常集中处理、权限验证、Web 参数校验、事务处理等等

    //TODO code小demo

  • 相关阅读:
    C++11 并发指南四(<future> 详解三 std::future & std::shared_future)(转)
    转: 关于 ssl的建立链接的过程
    工信部电信投诉网站入口
    rfc 标准文档目录
    转: 七牛云的开源播放器的使用指南
    转: Android基于HLS和RTMP协议的第三方SDK选择
    转:Android中Context详解 ---- 你所不知道的Context
    android开发推荐书籍列表
    转:java 类名 this 的使用
    转: android studio 消除SDK更新时的“https://dl-ssl.google.com refused”错误
  • 原文地址:https://www.cnblogs.com/zhaoyuan72/p/14802489.html
Copyright © 2020-2023  润新知