SpringBoot中aop的使用

步骤

  • 编写使用注解的被拦截类,加了这个注解的类或者方法就会被拦截
  • 使用@Aspect声明一个切面,并通过@Component让此切面成为Spring容器管理的Bean
  • 使用@After、@Befor、@Aroud定义建言(advice),可直接将拦截规则(切点)作为参数。这儿也是业务代码所在的地方
  • 在配置类上使用@EnableAspectJAutoProxy开启Spring对AspectJ的支持
  • 启动类上添加@AopConfig引入

部分代码

ExecTime.java

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExecTime {
    String value() default "";
}

ExecTimeAspect.java 切面

@Aspect
@Component
public class ExecTimeAspect {

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

    /**
     * 这儿填写ExecTime.java的全路径名
     */
    @Pointcut("@annotation(com.xx.annotation.ExecTime)")
    public void annotationPointCut() {
    }

    /**
     * 统计方法执行的时长
     *
     * @param joinPoint the join point
     * @return object
     * @throws Throwable
     */
    @Around("annotationPointCut()")
    public Object wasteTime(ProceedingJoinPoint joinPoint) {
        Object output = null;
        try {
            long start = System.currentTimeMillis();
            output = joinPoint.proceed();
            long elapsedTime = System.currentTimeMillis() - start;

            // 执行的真实类名称
            String className = joinPoint.getTarget().getClass().getSimpleName();
            logger.info(String.format("method [%s.%s()] execution time:%sms", className, joinPoint.getSignature().getName(), elapsedTime));
        } catch (Throwable throwable) {
            logger.error("aop record method exec time error", throwable);
        }
        return output;
    }
}

AopConfig.java

@Configuration
@ComponentScan("com.xxx)
@EnableAspectJAutoProxy
public class AopConfig {
}

启动类

@SpringBootApplication(scanBasePackages = "com.xxx")
@Import({AopConfig.class})
public class ApplicationStartUp extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ApplicationStartUp.class);
    }

    public static void main(String[] args) {
        APIVersionChecker.check();
        SpringApplication app = new SpringApplication(ApplicationStartUp.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}

实际使用

最后,在需要统计耗时的方法前,加上@ExecTime 注解就可以了
service方法:

    @ExecTime
    @Override
    public List<User> getList(Integer page, Integer pageSize) {
        return userRepository.findList((page-1)*pageSize, pageSize+1);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值