Java Spring Boot AOP不工作
我是Spring Boot和AOP的新手,我花了三天时间试图让它正常工作,但都没有用 我有一门课叫App。此类调用名为invokeRetrieveMethod的方法,该方法调用自动连线businessClass对象中的另一个方法。我只是试图记录运行带有自定义Java Spring Boot AOP不工作,java,spring,spring-boot,spring-aop,Java,Spring,Spring Boot,Spring Aop,我是Spring Boot和AOP的新手,我花了三天时间试图让它正常工作,但都没有用 我有一门课叫App。此类调用名为invokeRetrieveMethod的方法,该方法调用自动连线businessClass对象中的另一个方法。我只是试图记录运行带有自定义@LogExecutionTime注释的方法所需的时间,但运行代码时会出现空指针异常。请帮忙 @SpringBootApplication public class App { @Autowired BusinessClas
@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();
}
}
弹簧靴“豆”(?)
我的方面
@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 {
}
使用注释@enableSpectJautoproxy在App类中启用AspectJ
阅读更多信息:尝试在切入点表达式中使用完全限定类名:
@Around(@annotation(fully.qualified.LogExecutionTime)”)
您正在创建应用程序的新实例。您应该从上下文中获取实例并调用该实例上的方法。
@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 {
}