4

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.

Alex
  • 670
  • 11
  • 21

2 Answers2

6

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.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • 1
    Thanks for this - but working with a pre Spring legacy system; plugging in another project like Roo feels like overkill. – Alex Apr 03 '12 at 19:54
  • No your just going to have it make the pom file for you as that is the tricky part because you need the AspectJ compiler to run instead of the javac compiler. What is most likely happening is your not running the AspectJ compiler. – Adam Gent Apr 03 '12 at 20:07
  • Accepting this answer - looking at Roo it appears to use Aspectj to do its magic. (http://static.springsource.org/spring-roo/reference/html/architecture.html#architecture-critical-technologies-aspectj). The suggestion about mining a generated project for the library imports and project nature settings is a good one. – Alex Nov 07 '12 at 20:08
1

To use compile time weaving in maven, you need to use the aspectj compiler plugin. See this:

http://maven.apache.org/maven-1.x/plugins/aspectj/

To get your project working in Eclipse, you need to install the AspectJ project configurator for m2eclipse (assuming you are using m2eclipse). You can install it from going to Preferences -> Maven -> Discovery. Open the catalog and look for the AJDT configurator.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • Thats why I was going to have him install Spring STS because you can sometimes get the wrong combination of plugins (m2eclipse + ajdt). And if he creates a Roo project it will set this all up for him. – Adam Gent Apr 05 '12 at 01:52
  • STS would be the way to go in this case. – Andrew Eisenberg Apr 06 '12 at 02:34