简体   繁体   English

使用方法调用切入点在Spring中进行AspectJ加载时编织?

[英]AspectJ Load-time Weaving in Spring with method-call pointcuts?

I'm experimenting with AspectJ Load-time Weaving in Spring as described here . 我在Spring中使用AspectJ加载时编织试验所描述这里 I've checked out the sample code , and it works as advertised. 我已经签出了示例代码 ,它可以像宣传的那样工作。

But when I try to change the pointcut of the PerformanceAdvice from execution(..) to call(..) semantics, the advice no longer gets executed. 但是,当我尝试将PerformanceAdvice的切入点从execution(..)更改为call(..)语义时,该建议将不再执行。

I know that Spring AOP does not support call(..) semantics, but that should not apply here since I'm using AspectJ LTW (the sample code successfully works with non-Spring-managed objects). 我知道Spring AOP不支持call(..)语义,但是由于我使用的是AspectJ LTW(示例代码可成功地用于非Spring管理的对象),因此这在这里不适用。

Can anyone shed some light? 谁能阐明一些想法?

Update: In order to confirm that LTW works, I added the following system properties to the argline configuration in pom.xml: 更新:为了确认LTW是否有效,我在pom.xml的argline配置中添加了以下系统属性:

  • -Dorg.aspectj.weaver.showWeaveInfo=true
  • -Daj.weaving.verbose=true
  • -Dorg.aspectj.tracing.enabled=true
  • -Dorg.aspectj.tracing.factory=default
  • -Dorg.aspectj.tracing.file=/tmp/aspectj-trace.txt

Now the output of running mvn test contains the following lines: 现在,运行mvn test的输出包含以下几行:

[AppClassLoader] info AspectJ Weaver Version 1.5.3 built on Wednesday Nov 22, 2006 at 11:18:15 GMT
[AppClassLoader] info register classloader sun.misc.Launcher$AppClassLoader@5acac268
[AppClassLoader] info using configuration 
    /private/tmp/aspectj-load-time-weaving/target/test-classes/META-INF/aop.xml
[AppClassLoader] info register aspect org.springbyexample.aspectjLoadTimeWeaving.PerformanceAdvice
[AppClassLoader] weaveinfo Join point 
    'method-execution(void org.springbyexample.aspectjLoadTimeWeaving.Processor.process())' 
    in Type 'org.springbyexample.aspectjLoadTimeWeaving.Processor' (Processor.java:38) 
    advised by around advice from 
    'org.springbyexample.aspectjLoadTimeWeaving.PerformanceAdvice' (PerformanceAdvice.java)
[AppClassLoader] weaveinfo Join point 
    'method-execution(void org.springbyexample.aspectjLoadTimeWeaving.
        PerformanceAdvice.aspectjLoadTimeWeavingExamples())' 
    in Type 'org.springbyexample.aspectjLoadTimeWeaving.PerformanceAdvice' (PerformanceAdvice.java:37)
    advised by around advice from 
    'org.springbyexample.aspectjLoadTimeWeaving.PerformanceAdvice' (PerformanceAdvice.java)

So it seems that AspectJ has picked up the PerformanceAdvice and found 2 join points to weave it in. But if I replace execution(..) with call(..) in the PerformanceAdvice's join point and run maven again, the output does not contain those last 2 lines, and the advice is not executed. 因此,AspectJ似乎已经选择了PerformanceAdvice,并找到了2个连接点来进行编织。但是,如果我在PerformanceAdvice的连接点中将execution(..)替换为call(..)并再次运行maven,则输出将不包含最后两行,则不执行建议。 The AspectJ trace contains lots of log statements, but I couldn't make much sense out of it. AspectJ跟踪包含许多日志语句,但是我对此没有多大意义。 I scanned it for errors or warnings, but found none. 我扫描了它是否有错误或警告,但没有发现。

(I assume that I should be able to simply replace execution(..) with call(..) , because their syntax is described as execution(MethodPattern) and call(MethodPattern) respectively in the AspectJ programming guide ). (我假设我应该能够简单地用call(..)替换execution(..) ,因为在AspectJ编程指南中它们的语法分别描述为execution(MethodPattern)call(MethodPattern) )。

I also tried using a more recent version of AspectJ (1.6.6), but to no avail. 我也尝试使用更新版本的AspectJ(1.6.6),但无济于事。

The call designator should work with your pointcut. 呼叫指示符应与您的切入点一起使用。

Is your calling class in the package specified in the weaver element? 您的调用类是否在weaver元素中指定的包中?

<weaver>
        <!-- only weave classes in this package -->
        <include within="org.springbyexample.aspectjLoadTimeWeaving.*" />
</weaver>

If the calling and execution class is in different packages and it works with execution, then fix the aop.xml file or refactor your calling class to be in the given package. 如果调用和执行类位于不同的程序包中并且可以执行,则修复aop.xml文件或将调用类重构到给定的程序包中。

Are you using the actual aspectJ agent? 您是否在使用实际的AspectJ代理? Like passing to your JRE at runtime 就像在运行时传递给您的JRE

-javaagent:lib/aspectjweaver.jar

I found that this was the only way I was able to get aspectJ AOP rather than spring AOP 我发现这是我能够获得AspectJ AOP而不是春季AOP的唯一方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM