[英]Spring Boot AOP does not work
我是Spring Boot和AOP的新手,我花了最后三天的時間讓它無法正常工作。
我有一門叫App的課。 此類調用一個稱為invokeRetrieveMethod的方法-該方法將在自動裝配的businessClass對象中調用另一個方法。 我只是試圖記錄運行帶有自定義@LogExecutionTime
批注的方法所花費的時間,但是在運行代碼時出現空指針異常。 請幫忙!
@SpringBootApplication
public class App
{
@Autowired
BusinessClass businessClass;
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println("Starting application...");
App app = new App();
app.invokeRetrieveSomething();
}
public void invokeRetrieveSomething() {
businessClass.retrieveSomething();
}
}
春季啟動“豆”(?)
@Component
public class BusinessClass {
@LogExecutionTime
public void retrieveSomething() {
System.out.println("This is the retrieveSomething() method.");
}
}
我的觀點
@Aspect //specifies that this is an aspect
@Component //because we want this class to be turned into a bean in order for it to work supposedly
public class ExampleAspect {
@Around("@annotation(LogExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis(); //executed before the method annotated with @LogExecutionTime is executed.
Object proceed = joinPoint.proceed();
//everything below gets executed after the method.
long executionTime = System.currentTimeMillis() - start;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return proceed;
}
}
我的自定義注釋
@Target(ElementType.METHOD) //tells us *where* this annotation will be applicable (ElementType.METHOD means on methods only)
@Retention(RetentionPolicy.RUNTIME) //states whether the annotation will be available to the jvm at runtime or not. by default, it's not.
public @interface LogExecutionTime {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.