随笔 - 836  文章 - 1 评论 - 40 阅读 - 102万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

使用AOP 自动打印方法进出, 耗时 logger

 

依赖:

复制代码
   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
   </dependency>

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
   </dependency>
复制代码

 

注解:

复制代码
/**
 * *************************************************************************
 * <PRE>
 *  @ClassName:    : Mylog 
 *
 *  @Description:    : 自定义注解, 实现对方法的进 出 ,运行时间 打印logger,
 *
 *  @Creation Date   : 8 Aug 2019 3:02:36 PM
 *
 *  @Author          :  Sea
 *  
 *
 * </PRE>
 **************************************************************************
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Mylog { 
    String value() default "";
}
复制代码

 

 

 

切片:

复制代码
@Aspect
@Component
public class MylogAspect {

    private static Logger logger = LoggerFactory.getLogger(MylogAspect.class);


    @Around("@annotation(Mylog)") // 作用到注释@Mylog标记的方法上
    public Object handleSeaAnnotionAOPMethod(ProceedingJoinPoint joinPoint, Mylog Mylog) throws Throwable {

        long start = System.currentTimeMillis();
        // 获取方法的 全类名 com.sea.controller.XXXcontroller.getdate()
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String className = signature.getDeclaringType().getSimpleName();

        // 获取注解作用方法名 eg:getdata
        String methodName = joinPoint.getSignature().getName();

        // 获取方法--
//                 Method method = joinPoint.getTarget().getClass().getMethod(methodName, signature.getParameterTypes());
//                 Method method = getMethod(joinPoint);

        logger.info("&&&&&& enter into the class {} -> method {} &&&&&&", className, methodName);

        // ################################################################
        Object object = joinPoint.proceed();

        System.err.println(className);
        long end = System.currentTimeMillis();
        logger.info("&&&&&& end  the class {} -> method {} &&&&&&", className, methodName);
        logger.info(" ^^^^^^^^^run the  method {} total cost time : {}  ms ^^^^^^^^",methodName, (end - start));
        return object;
    }}
复制代码

 

 

Test:

    @GetMapping("/get")
    @Mylog
     public String getData() {
        
        
         return "nihao"+ UUID.randomUUID();
    }
    

 

结果:  .....

 

posted on   lshan  阅读(1043)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示