未调用第二个带注释的方面



我正试图了解Spring AOP,但目前面临的问题是,TrackedSubTask注释的第二个方面没有被调用,而Tracked可以工作。感谢任何帮助或线索。

以下是我的注释类:

@RestController
public class Demo {
@Tracked
@RequestMapping("/")
public String index() {
DemoImpl bar = new DemoImpl();
bar.foo();
return "Greetings from Spring Boot!";
}
}
public class DemoImpl{
@TrackedSubTask
public void foo() {
System.out.println("foo");
}
}

以下是我的注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Tracked  {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackedSubTask {
}

以下是我的方面:

@Aspect
@Component
class MyAspect {
@Around("@annotation(annotation)")
public Object first(ProceedingJoinPoint joinPoint, Tracked annotation) throws Throwable {
Object proceed = joinPoint.proceed();
return proceed;
}
@Around("@annotation(subTaskAnnotation)")
public Object second(ProceedingJoinPoint joinPoint, TrackedSubTask subTaskAnnotation) throws Throwable {
Object  proceed = joinPoint.proceed();
return proceed;
}
}

正如M.Deinum正确指出的那样,DemoImpl需要由Spring管理。

最新更新