I have a java application build upon Spring 3. This project has another jar as a dependency.
This dependency contains a @org.aspectj.lang.annotation.Aspect
I have the same problem. I solved this problem packaging with maven. Check the aspectj-maven-plugin
and option weaveDependency
http://mojo.codehaus.org/aspectj-maven-plugin/weaveJars.html
You might try aspectJ LTW instead of Spring AOP proxy. To do this add a aop.xml to your META-INF
<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in this package -->
<include within="org.springbyexample.aspectjLoadTimeWeaving.*" />
</weaver>
<aspects>
<!-- use only this aspect for weaving -->
<aspect name="org.springbyexample.aspectjLoadTimeWeaving.PerformanceAdvice" />
</aspects>
</aspectj>
And this is the spring portion of the config:
See here for details : http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-aj-ltw
From AspectJ in action book: aspects used with the proxy-based AOP (declared using @AspectJ or XML-based syntax) are Spring beans and shouldn’t use the aspectOf() approach to instantiation.
Declare it normally and see if it works out:
<bean id="myAspect" class="com.project.MyAspect">
<property name="someproperty" value="somevalue" />
</bean>
I've ended up declaring the aspects in the spring's applicationContext xml config and removing the annotations.
What was working so far was using the aspectj plugin for maven, but everytime I changed a class in eclipse, I had to run $ mvn compile
(because eclipse doesn't know the aspects, and was compiling the classes without them), and that's an awful thing to say to anybody that will use MyAspect
.
Then I just created a config file and documented: to use MyAspect
, just import this config rules to your spring's context configuration.
take a look at ApectWerks, it does load-time weaving: http://aspectwerkz.codehaus.org/weaving.html
Considering it works perfectly fine when the classes are packaged with the application and spring I can only think it would be a classloading issue.
If it works fine when your bundled in your app then then when AOP scans all the classes that it will have to monitor then it is referencing the right classloader with all the right jars. But now when you remove it and set it into a JAR it is scanning under the classloader with all of the other third party jars.
I am not 100% sure how it is mapped out but it could be something like this:
Bootstrap Classloader <- Third Party Classloader <- Application Class Loader (with all your classes)
\ \
aspectj.jar spring.jar
If its aspect.jar is scanning under only its classloader then it will not be able to see 'all your classes'. One way you can try confirming this is to get a heap dump of your app. Run it against Eclipse MAT, Check out Class Loader explorer and look for the aspect classes. If they do not reside under the same classloader as your application you would have to look at a way to have tomcat tell the third party libraries of the application classes.