春季AOP-切入点未得到调用 [英] Spring AOP - Point Cut not getting called

查看:208
本文介绍了春季AOP-切入点未得到调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SpringBoot应用程序. 我已经定义了一个注释说"Track",并且在不同的程序包中我注释了几种方法,我希望aop考虑这些方法. 注释已定义如下:

I have a SpringBoot Application. I have defined an Annotation say "Track", and I have annotated few methods in different packages which I want aop to consider. The annotation has been defined as below :

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Track {

}

我没有错过我软件包的@Configuration类中的@EnableAspectJAutoProxy.

I have not missed the @EnableAspectJAutoProxy in the @Configuration class of my package.

我在Aspect中定义了切入点和建议,如下所示:

I have a Pointcut and an Advice defined in the Aspect like below :

@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注释),任何访问修饰符,任意数量的输入参数和返回类型,都要遵循方面的@Around建议.

现在,有趣的情况如下: 我有一个说"Engine"的类,它调用其他类和下游系统来执行长时间运行的操作.让我们定义类如下:

Now, the interesting situation is as below : I have a class say "Engine" which calls other classes and downstream systems to perform a long-running operation. Let's define the class as follows :

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
 }
}

所有其他"自动装配对象的方法都将通过切入点按预期方式识别,但是此Engine类中已使用@Track注释的方法无法识别.什么是谜?

All the "other" autowired objects' methods are getting recognized by pointcut as expected but the method within this Engine class, that has been annotated with @Track, is not recognized. What's the mystery?

我尝试将"someMethodOfEngineClass"方法公开,返回一些内容而不是void和所有这些组合,并且不起作用.

I have tried making "someMethodOfEngineClass" method public, return something instead of void and all those combinations and it doesn't work.

我想念什么? 是切入点定义表达式吗? 我已经在一个子包中定义了方面,应该在包结构的顶层定义方面吗?

What am I missing? Is it the pointcut definition expression? I have defined the aspect in one of the sub packages, is aspect supposed to be defined at the top level in the package structure?

您能否提出一些可行的建议?我有点被困住了.

Can you folks please suggest something that can work? I am kinda stuck at this.

推荐答案

定义aop spring会在该类周围创建代理时, 因此,在调用该方法时,实际上将调用委派给代理,例如

When you define aop spring creates proxy around the class, so when the method is called, actually call is delegated to proxy, sth like

your.package.Engine$$FastClassBySpringCGLIB$$c82923b4.someMethodOfEngineClass()

但这仅在从类的外部调用方法时有效 如果您从同一类调用类方法,则实际上是通过this.someMethodOfEngineClass()

But this works only when a method is called from outside it's class If you call class method from the same class you are effectively calling it by this.someMethodOfEngineClass()

此处-> http://www.nurkiewicz.com/2011/10/spring-pitfalls-proxying.html 您可以找到有关代理的更多信息

here -> http://www.nurkiewicz.com/2011/10/spring-pitfalls-proxying.html you can find more info about proxying

因此代理被绕过并且aop无法正常工作.

so proxy is bypassed and aop is not working.

这篇关于春季AOP-切入点未得到调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆