Spring AOP - 切点未被调用
Spring AOP - Point Cut not getting called
我有一个 SpringBoot 应用程序。
我已经定义了一个注释,比如 "Track",并且我在不同的包中注释了几个我希望 aop 考虑的方法。
注释定义如下:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Track {
}
我没有错过我包的@Configuration class 中的@EnableAspectJAutoProxy。
我在切面中定义了一个切入点和一个建议,如下所示:
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(@Track * *.*(..))")
void annotatedMethod() {
// No Implementation required
}
@Around("annotatedMethod() && @annotation(methodLevelTrack)")
public void adviseAnnotatedMethods(ProceedingJoinPoint proceedingJoinPoint,
Track methodLevelTrack) throws Throwable {
// do some task
proceedingJoinPoint.proceed();
// do some task after the method is executed.
}
}
我的意图是:对于任何包中的任何方法(用@Track 注释)、任何访问修饰符、任何数量的输入参数和任何 return 类型,都遵循方面的@Around 建议。
现在,有趣的情况如下:
我有一个 class say "Engine" 调用其他 classes 和下游系统来执行 long-运行 操作。让我们定义 class 如下:
public class Engine {
// bunch of other autowired objects
public void processTask() {
<autowired_object_A>.someMethod() // this method has been annotated with @Track
<autowired_object_B>.someMethod() // this method has also been annotated with @ Track
.... // bunch of other methods in other autowired objects that have been annotated with @ Track
someMethodOfEngineClass(); // Now this has been defined in the Engine class as below, but pointcut doesn't recognize this method!
}
@Track
private void someMethodOfEngineClass() {
// do something
}
}
所有 "other" 自动装配的对象的方法都按预期被切入点识别,但此引擎 class 中已用 @Track 注释的方法未被识别。有什么奥秘?
我已经尝试制作 "someMethodOfEngineClass" 方法 public、return 一些东西而不是 void 和所有这些组合,但它不起作用。
我错过了什么?
它是切入点定义表达式吗?
我已经在其中一个子包中定义了方面,是否应该在包结构的顶层定义方面?
各位大佬能否推荐一些可行的方法?我有点卡在这上面了。
当你定义 aop spring 围绕 class 创建代理时,
所以当方法被调用时,实际上调用被委托给了代理,比如
your.package.Engine$$FastClassBySpringCGLIB$$c82923b4.someMethodOfEngineClass()
但这只有在从外部调用方法时才有效 class
如果您从同一个 class 调用 class 方法,您实际上是通过 this.someMethodOfEngineClass()
调用它
这里 -> http://www.nurkiewicz.com/2011/10/spring-pitfalls-proxying.html
您可以找到有关代理的更多信息
所以代理被绕过,aop 不工作。
我有一个 SpringBoot 应用程序。 我已经定义了一个注释,比如 "Track",并且我在不同的包中注释了几个我希望 aop 考虑的方法。 注释定义如下:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Track {
}
我没有错过我包的@Configuration class 中的@EnableAspectJAutoProxy。
我在切面中定义了一个切入点和一个建议,如下所示:
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(@Track * *.*(..))")
void annotatedMethod() {
// No Implementation required
}
@Around("annotatedMethod() && @annotation(methodLevelTrack)")
public void adviseAnnotatedMethods(ProceedingJoinPoint proceedingJoinPoint,
Track methodLevelTrack) throws Throwable {
// do some task
proceedingJoinPoint.proceed();
// do some task after the method is executed.
}
}
我的意图是:对于任何包中的任何方法(用@Track 注释)、任何访问修饰符、任何数量的输入参数和任何 return 类型,都遵循方面的@Around 建议。
现在,有趣的情况如下: 我有一个 class say "Engine" 调用其他 classes 和下游系统来执行 long-运行 操作。让我们定义 class 如下:
public class Engine {
// bunch of other autowired objects
public void processTask() {
<autowired_object_A>.someMethod() // this method has been annotated with @Track
<autowired_object_B>.someMethod() // this method has also been annotated with @ Track
.... // bunch of other methods in other autowired objects that have been annotated with @ Track
someMethodOfEngineClass(); // Now this has been defined in the Engine class as below, but pointcut doesn't recognize this method!
}
@Track
private void someMethodOfEngineClass() {
// do something
}
}
所有 "other" 自动装配的对象的方法都按预期被切入点识别,但此引擎 class 中已用 @Track 注释的方法未被识别。有什么奥秘?
我已经尝试制作 "someMethodOfEngineClass" 方法 public、return 一些东西而不是 void 和所有这些组合,但它不起作用。
我错过了什么? 它是切入点定义表达式吗? 我已经在其中一个子包中定义了方面,是否应该在包结构的顶层定义方面?
各位大佬能否推荐一些可行的方法?我有点卡在这上面了。
当你定义 aop spring 围绕 class 创建代理时, 所以当方法被调用时,实际上调用被委托给了代理,比如
your.package.Engine$$FastClassBySpringCGLIB$$c82923b4.someMethodOfEngineClass()
但这只有在从外部调用方法时才有效 class
如果您从同一个 class 调用 class 方法,您实际上是通过 this.someMethodOfEngineClass()
这里 -> http://www.nurkiewicz.com/2011/10/spring-pitfalls-proxying.html 您可以找到有关代理的更多信息
所以代理被绕过,aop 不工作。