views:

65

answers:

2

I'm experimenting with AspectJ Load-time Weaving in Spring as described here. 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.

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).

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:

  • -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:

[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. The AspectJ trace contains lots of log statements, but I couldn't make much sense out of it. 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).

I also tried using a more recent version of AspectJ (1.6.6), but to no avail.

A: 

Are you using the actual aspectJ agent? Like passing to your JRE at runtime

-javaagent:lib/aspectjweaver.jar

I found that this was the only way I was able to get aspectJ AOP rather than spring AOP

bwawok
Yes, the agent is configured in the maven POM (see below). Since I can sucessfully advice non-Spring-managed objects, AspectJ seems to be working.http://svn.springbyexample.org/core/aspectj-load-time-weaving/tags/1.0/pom.xml
sapporo
+2  A: 

The call designator should work with your pointcut.

Is your calling class in the package specified in the weaver element?

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

Espen
Thanks for your comment. I double checked that AspectJ actually kicks in, and updated my question accordingly.
sapporo
Is the (test) class that calls on Processor.process() in this package: `org.springbyexample.aspectjLoadTimeWeaving`?
Espen
Yes it is, see the URL below for source code. And if it wasn't, I suppose AspectJ wouldn't work with "execution" pointcut either.http://svn.springbyexample.org/core/aspectj-load-time-weaving/tags/1.0/src/test/java/org/springbyexample/aspectjLoadTimeWeaving/ProcessorTest.java
sapporo
No, with the call designator, your test class will be weaved, while with the execution designator, the Processor class will be weaved. The [AJDT Eclipse plugin](http://eclipse.org/ajdt/ "AJDT Eclipse Plugin") shows the pointcuts with orange arrows in the editor and is very helpful!
Espen
You're right of course, thanks. AJDT is a good idea, I will give that a try.
sapporo