How to enable compile-time aspectj weaving for Eclipse embedded Tomcat

Alex picture Alex · Apr 3, 2012 · Viewed 9k times · Source

I'm having a problem trying to make eclipse and aspectj work for Dynamic Web Projects. I'm looking for compile time weaving so I can use the Eclipse Visualisation features.

I've followed the steps given here:

https://forums.oracle.com/forums/thread.jspa?messageID=8591748

using Eclipse Indigo (3.7) with the latest Aspectj eclipse plugin (2.1.3).

The steps were as follows:

[1] Create basic servlet

//imports omitted
public class MyServlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response){
   PrintWriter out= null;
   try {
      out = response.getWriter();
      out.write("hello from MyServlet");    
   } catch (IOException e) {
      e.printStackTrace();
   } finally {
      if(out!=null)
      out.close();
   }
}
}

[2] Add servlet to deployment discriptor (web.xml)

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee      
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

    <servlet>
       <servlet-name>MyServlet</servlet-name>
       <servlet-class>com.myCompany.MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
       <servlet-name>MyServlet</servlet-name>
       <url-pattern>/MyServlet/*</url-pattern>
    </servlet-mapping>

</web-app>

[3] Create aspect

package com.myCompany;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public aspect MyServletAspect {

  pointcut doGet() :
     execution(public void MyServlet.doGet(HttpServletRequest,HttpServletResponse));

  after() returning : doGet() {
     System.out.println("Hello from doGet (via aspectj)");
  }
}

However when I run this no dice - the aspect just doesn't run (servlet writes to response, aspect to console). I did something similar for a regular java project and that works fine.

I note there are guidelines for adding aop.xml into the web app's META-INF directory, however this didn't work for me.

Our goal is to run aspectj non-invasively in development via eclipse for a non-Spring framework (or Maven) project - this should be easy.. but I haven't been able to make it work.

Any suggestions / reference to tutorial for compile-time weaving for web apps in eclipse would be useful. The app server is embedded Tomcat 6 (but can be upgraded to tomcat 7 if required).

The ability to tweak the development environment at runtime without impacting the production code would be great - if it can be made to work. Responses much appreciated.

Answer

Adam Gent picture Adam Gent · Apr 3, 2012

I recommend you just download and use Spring STS (Spring's Eclipse) and download/create a Spring Roo project.

Your just going to use the Roo project to boostrap your own project with the correct AspectJ libraries. That is you'll just use its pom file that it generates. You can try to use plain Eclipse and download all the plugins (which is what I do) but its PITA to get everything setup correctly.

The key thing is to get the AspectJ compiler to run instead of the regular Java compiler. This requires a special Maven plugin or Ant plugin. Also you do not need the aop.xml file.

If your using Eclipse you need to make sure that the AspectJ nature is added to the project (usually you right click on the project and select "add natures" "Or convert to...".)

You'll also in Eclipse need to add the Spring Aspects jar to the "Aspect Libraries" which is not the classpath/buildpath.