我正在设法使自己了解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管理。

10-06 05:56