步骤

  • 编写使用注解的被拦截类,加了这个注解的类或者方法就会被拦截
  • 使用@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);
    }
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区