Aspectj : deploying aspects in an existing web application

1.7k Views Asked by At

I am working on an existing web application that uses EJB3. What I need to do is to weave some aspects into this application, in order to log some information after matching some specific methods (before and after persisting beans).

I am deploying my web application in a Jboss server in an ear format. the ear contains a war (servlets) and a jar (session beans and entity beans).

Any idea on how is this possible ? Do I have to put my aspects inside the jar or do I have to create a new jar (exclusively for aspects) ?

By the way I am using eclipse for this project. So my jar corresponds to an EJB project and my war corresponds to a dynamic web project.

Thank you for your answers

1

There are 1 best solutions below

5
On BEST ANSWER

As far as I know for web-apps it's only possible to do so with load-time weaving (shouldn't be mixed with compile-time one, they are not compatible with each other). To achieve that you should do the following:

  • Create aop.xml (link to the documentation how to do that) and put it into the META-INF folder of your project.

  • add aspectjweaver/tools/rt jars to your dependencies.

  • disable compile-time weaving (That's a configuration in your pom.xml. Read more there)

  • add to your app-server's JVM the following argument -javaagent:pathto/aspectjweaver.jar (documentation)

  • add the aspectjweaver.jar to your server's resources

That's it.

You should disable compile-time weaving im your pom.xml (if you use maven, of course). Unfortunately I didn't try to do that without maven (solely with Eclipse AspectJ plugin). Here is how it will probably look like.

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <!-- disabling -->
                    <outxml>true</outxml>
                    <XterminateAfterCompilation>true</XterminateAfterCompilation>
                    <!-- disabling -->
                </configuration>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
  </build>