SpringBoot AOP注解方式监控方法执行时间
源码中提供了CatAnnotation 以及 CatAopService 这两个类可以参照这两个类
1、在pom.xml中引入AOP的支持
<!-- SpringBoot对AOP的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、书写CatAnnotation注解类
@Retention(RUNTIME)
@Target(ElementType.METHOD)
public @interface CatAnnotation {
}
3、书写CatAopService AOP类
@Aspect
@Component
public class CatAopService {
@Around("@annotation(catAnnotation)")
public Object aroundMethod(ProceedingJoinPoint pjp, CatAnnotation catAnnotation) {
MethodSignature joinPointObject = (MethodSignature) pjp.getSignature();
Method method = joinPointObject.getMethod();
Transaction t = Cat.newTransaction("method", method.getName());
try {
Object result = pjp.proceed();
t.setStatus(Message.SUCCESS);
return result;
} catch (Throwable e) {
t.setStatus(e);
Cat.logError(e);
} finally {
t.complete();
}
return null;
}
}
4、在相应的方法上面加上@CatAnotation注解

5、观察CAT监控页面

网友评论