基于spring、aspectj和注释的性能方法审计
我有一个关于如何使用注释、aspectj和spring对方法进行时间性能审计的问题 基本上,我有:基于spring、aspectj和注释的性能方法审计,spring,annotations,aop,aspectj,Spring,Annotations,Aop,Aspectj,我有一个关于如何使用注释、aspectj和spring对方法进行时间性能审计的问题 基本上,我有: public class MyClass{ @TimeAudit public myMethod(){ //do something } } 我只想在控制台中的某个地方把它打印出来,显示执行该方法所花费的时间。我的问题是一个方面将如何截取该注释,并在截取之后计算该方法所花费的时间 我怎么能做到? 我想澄清一下我的问题: 我有注释: @Retention(RetentionPolic
public class MyClass{
@TimeAudit
public myMethod(){
//do something
}
}
我只想在控制台中的某个地方把它打印出来,显示执行该方法所花费的时间。我的问题是一个方面将如何截取该注释,并在截取之后计算该方法所花费的时间
我怎么能做到?
我想澄清一下我的问题:
我有注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface TimeAudit {
}
我有我的方面:
@Aspect
@Component
public class PerformanceTimeExecutionAudit {
@Around("execution(* *(..)) && @annotation(timeAudit)")
public Object doLogTime(final ProceedingJoinPoint pjp, TimeAudit timeAudit) throws Throwable {
System.out.println("Start time..."+System.currentTimeMillis());
Object output = pjp.proceed();
System.out.println("End time..."+System.currentTimeMillis());
return output;
}
}
其他类别:
@Repository
public class MyClass{
@Override
@TimeAudit
public void myMethod(){
//do something
}
}
但是,如果我将@TimeAudit放入该方法,则不会触发该方面。
我做错了什么?总结一个简短的教程,介绍如何结合注释创建一个方面,以便对该领域的新手有用 您需要库依赖项: aspectjrt aspectjweaver 春季aop 以及其他spring依赖项,如spring上下文等。 2创建注释示例:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface TimeAudit {
//put here whatever fields you need
}
3创建您的方面,例如:
@Aspect
@Component
public class PerformanceTimeExecutionAudit {
@Around("execution(* *(..)) && @annotation(TimeAudit)")
public Object doLogTime(final ProceedingJoinPoint pjp, TimeAudit timeAudit) throws Throwable {
System.out.println("Start time..."+System.currentTimeMillis());
Object output = pjp.proceed();
//this is with @Around, you can use in your asspect all others annotations like @Before, @After etc. this depends on your logic behavior.
System.out.println("End time..."+System.currentTimeMillis());
return output;
}
}
4在你的方法上,你可以像这样使用你的注释——一点观察是,你可以创建你想要的注释
@Repository
public class MyClass{
@Override
@TimeAudit
public void myMethod(){
//do something
}
}
//- this @TimeAudit can contain params, this depends on your Annotation logic creation
确保spring上下文正在扫描包含方面的包,以及包含注释类的包。或者您可以在spring上下文配置中将它们声明为bean
确保已启用AOP。在spring配置中,您需要这样的内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="........
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=".........
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy />
就这样。
我希望它对某些人有用。。谢谢你,它似乎可以用来衡量绩效。但是我想看看我怎么做,你可以检查一下。@Profiled注释位于aop包中。这将触发同样在同一个包中的ProfiledTimingSpect。从这里,您可以按照代码了解如何处理实际测量和日志记录。AspectJ配置在我上面链接的页面上。您在Spring配置中启用了AOP吗?应用程序是如何配置的?是。。。。xmlns:aop=