自定义注解、切面,监控每个controller执行时长

ExecuteTime注解

@Target(ElementType.METHOD)//注解用在方法上,用于描述方法
@Retention(RetentionPolicy.RUNTIME)//注解在运行时保留
@Documented//指定javadoc生成API文档时显示该注解信息
public @interface ExecuteTime {
    //调用时长阈值,默认为0,单位为毫秒,运行时长超过threshold毫秒时打印运行时长日志
    int threshold() default 0;

    String msg() default "";

    String postfix() default "接口---执行时长:{}毫秒";

    //日志级别
    LogLevel logLevel() default LogLevel.info;
}

InvokeTimeAdvice

@Aspect
public class InvokeTimeAdvice {

    @Pointcut("execution(public * com.htjc..*.*(..))&&@annotation(executeTime)")
    public void pointCut(ExecuteTime executeTime) {
    }

    ;

    @Around("pointCut(executeTime)")
    public Object logInvokeTime(ProceedingJoinPoint joinPoint, ExecuteTime executeTime) throws Throwable {
        long start = System.currentTimeMillis();//开始时间
        final Logger LOGGER = LoggerFactory.getLogger(joinPoint.getTarget().getClass());
        try {
            return joinPoint.proceed();
        } finally {
            long end = System.currentTimeMillis();//结束时间
            long duration = end - start;
            if (duration >= executeTime.threshold()) {
                String msg = executeTime.msg();
                if (msg == null || msg.trim().length() == 0) {
                    String methodName = joinPoint.getSignature().getName();//方法名
                    LogUtils.log(LOGGER, executeTime.logLevel(), "方法[{}]执行时长:{}毫秒", methodName, duration);
                }
            } else {
                LogUtils.log(LOGGER, executeTime.logLevel(), executeTime.msg() + executeTime.postfix(), duration);
            }
        }
    }
}

advice bean注入applicationContext.xml

<!--切面 start-->
    <!--记录方法执行时长-->
    <bean id="invokeTimeAdvice" class="com.common.aspect.InvokeTimeAdvice"></bean>
   
    <!--切面 end-->

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以简单地为您介绍一下在 Spring 中如何定义注解切面。 首先,您需要使用 Spring AOP 实现切面编程。Spring AOP 是基于代理模式的 AOP 框架,它可以在不修改原有代码的情况下,通过动态代理的方式对方法进行增强。 其次,您需要定义一个自定义注解,例如: ``` @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default ""; } ``` 这个注解可以在方法上进行标注,用来表示需要被切面增强的方法。 接下来,您可以定义一个切面类,实现对 MyAnnotation 注解进行切面增强,例如: ``` @Aspect @Component public class MyAspect { @Pointcut("@annotation(com.example.demo.MyAnnotation)") public void myPointcut() {} @Around("myPointcut()") public Object around(ProceedingJoinPoint pjp) throws Throwable { // 在方法执行前进行增强 System.out.println("before method execute..."); // 执行原有方法 Object result = pjp.proceed(); // 在方法执行后进行增强 System.out.println("after method execute..."); return result; } } ``` 在这个切面类中,我们使用 @Pointcut 定义了一个切点,表示需要增强被 MyAnnotation 注解标注的方法。在 around 方法中,我们可以在方法执行前后进行增强操作。 最后,您需要在 Spring 配置文件中将切面类注册为 Bean,并开启 AOP 自动代理,例如: ``` @Configuration @EnableAspectJAutoProxy @ComponentScan(basePackages = "com.example.demo") public class AppConfig { @Bean public MyAspect myAspect() { return new MyAspect(); } } ``` 这样,当您使用 MyAnnotation 注解标注一个方法时,该方法就会被 MyAspect 切面类增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值