• java自定义注解的使用-基于AOP的自定义日志配置


    话不多说直接上代码:

    注解类:

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface MyLog {
    
    }

    代理类:

    @Component
    @Aspect
    public class LogAspects {
    @Pointcut("@annotation(com.example.demo.aop.MyLog)")
    public void pointCut() {
        
    }
    @Before("pointCut()")
    public void beforeMethod() {
        System.out.println("before方法执行中。。。。。。");
    }
    @After("pointCut()")
    public void afterMethod() {
        System.out.println("after方法执行中。。。。。。");
    }
    @AfterReturning("pointCut()")
    public void afterReturningMethod() {
        System.out.println("afterReturning方法执行中。。。。。。");
    }
    @AfterThrowing("pointCut()")
    public void afterThrowingMethod() {
        System.out.println("afterThrowing方法执行中。。。。。。");
    }
    @Around("pointCut()")
    public Object aroundThrowingMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("Around之前的方法执行中。。。。。。");
        Object o = proceedingJoinPoint.proceed();
        System.err.println(o);
        System.out.println("Around之后的方法执行中。。。。。。");
        return o;
    }
    }

    controller层:

    @RestController
    public class LogTestController {
        @MyLog
        @GetMapping("/logTest")
        public String logTest(@RequestParam("id") int id, @RequestParam("name") String name) {
            System.out.println("id:" + id + "   " + "name:" + name);
            return id+name;
        }
    }

    说白了就是那个方法上标注了MyLog注解就代理谁

  • 相关阅读:
    Go安装
    Redis 安装与使用
    scala总结
    C++学习笔记4
    LeetCode 22.将数组分成和相等的三个部分
    LeetCode 21.二叉树的直径 DFS深度遍历
    LeetCode 20.买卖股票的最佳时机 暴力破解法与动态规划
    LeetCode 19.凑零钱问题 动态规划
    LeetCode 18.队列的最大值
    Java SSM Spring MVC 三层架构和MVC+SpringMVC的入门案例+请求参数的绑定+常用的注解
  • 原文地址:https://www.cnblogs.com/mcjhcnblogs/p/13210449.html
Copyright © 2020-2023  润新知