Some weeks ago I wrote a custom JSP tag for a Spring project I am currently working on. Inside the tag I wanted to use a Spring bean. Soon I realized that this case had to be handled a bit different because the tag is instantiated by the application server and not from the Spring context. Therefore the simple standard injection mechanism did not work.
Using the Spring documentation I found out that I had to use AspectJ for dependency injection in this case. I added the @Configurable annotation to my tag class. I added the required configurations to the XML files:
1 2 | <context:spring-configured /> <bean class="net.einwaller.spring.Tag" lazy-init="true" /> |
To get the required classes in the classpath of my application I had to add the libraries aspectjrt.jar and spring-aspects.jar. The first approach I took for weaving the aspects into my classes was load time weaving. I achieved this by adding the following to the VM arguments of my Tomcat instance:
-noverify -javaagent:pathtojar\aspectjweaver.jar
The noverify option was needed because the version of log4j caused a problem when starting the server without the option. I configured the path to my aspectjweaver.jar as an absolut path.
Having done all these steps I got my application running again but the load time weaving was a bit annoying. I was not able to change the code of my application while the server was running. The Java VM hot code replace did not work anymore. Restarting the server for every change slows down development performance a lot. So I searched for a better solution.
First I installed the AspectJ Eclipse plugin. I wanted to use the AspectJ compiler for compile time weaving of the AnnotationBeanConfigurerAspect. To do so I had to convert the project into an AspectJ project which changes the compiler and adds some project preference pages (RMB -> AspectJ Tools -> Convert to AspectJ Project).
Afterwards I had tried to add the spring-aspects.jar to the Inpath of the AspectJ Build preferences but that did not work. I moved the JAR file out of the WEB-INF/lib folder and tried to add it again - and it worked! Seems like the AspectJ plugin has some problems withe the classpath of the WTP plugin.
Now I got the shiny AspectJ markers at the top of the classes I marked with the @Configurable annotation. The application runs inside the Tomcat after removing the VM arguments for load time weaving and hot code replace works again - perfect!





March 7th, 2008 at 4:22 am
I worked outa hack that you can use to keep the spring-aspects jar in WEB-INF/libs. Change your .classpath file to look like this:
March 7th, 2008 at 8:21 am
hey btiernay - I would be interested in you classpath, seems there has something gone wrong posting it! please try again.