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注解就代理谁

posted @   码在江湖  阅读(355)  评论(0编辑  收藏  举报
编辑推荐:
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
阅读排行:
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 接口重试的7种常用方案!
点击右上角即可分享
微信分享提示