Announcement Announcement Module
Collapse
No announcement yet.
Getting AspectJ working in a Spring environment Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting AspectJ working in a Spring environment

    We are using jetty as a servlet container, the Spring Framework for security and some other stuff, and now trying to integrate some AspectJ for creating an Audit Trail. We want to use load time weaving for our aspects.

    Our code is broken down as follows
    core.jar - core functionality
    security.jar - basic security
    security_audit.jar - aspects that audit security actions
    functionality.jar - server functionality
    functionality_audit.jar - aspects that audit functionality actions
    webapp.war - the webapp

    The goal is to have the audit.jar be optional, so if we drop it in, auditing is being performed via load time weaving, if it is not present, no auditing.

    In eclipse I have these all broken into separate projects that create the components as described above.

    I want to add a side note that I am not very familiar with spring. All the spring setup was performed by another developer, I am just inheriting this and need to get it to work. I have not been able to get any aspects to work in this environment yet. However I am somewhat familiar with writing aspects.

    In my functionality_audit project I have a src/main/resources directory with a file named functionality_audit.xml and the following xml
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <!-- this switches on the load-time weaving -->
        <context:load-time-weaver aspectj-weaving="on"/>
    </beans>
    and in the same directory there is a directory named META-INF with an aop.xml file with the following contents
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE aspectj PUBLIC    
      "-//AspectJ//DTD//EN"    "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">    
    
    <aspectj>    
       <weaver options="-showWeaveInfo">
    	 <include within="com.myco.myapp.*"/>
       </weaver>
       <aspects>
      	 <aspect name="com.myco.myapp.audit.AuditLoggingAspect"/>
       </aspects>
    </aspectj>
    And then in src/main/java is the full package structure to an aj file. This successfully compiles in a jar with a class file and the other 2 xml files are in the jar file as well.

    FYI: We use maven as a build build tool

    No changes have been made to the functionality.jar project. That continues to work as desired.

    In the webapp, we place the functionality_audit.jar file in the WEB-INF/lib directory so that it is sitting side by side with functionality.jar and all the spring libraries including the springsource.org.aspectj.runtime and weaver version 1.6.3. I also just noticed that there is another aop.xml file in the META-INF with the following contents
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE aspectj PUBLIC    
      "-//AspectJ//DTD//EN"    "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">    
    
    <aspectj>    
       <weaver options="-showWeaveInfo
                  -XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler">
    	 <include within="com.myco.myapp.audit.AuditLoggingAspect" />
       </weaver>
       <aspects>
         <include within="com.myco.myapp.audit.AuditLoggingAspect"></include>
       </aspects>
    </aspectj>
    As I mentioned earlier we are using jetty and there is a web-override.xml in the webapp project. I added a reference to the functionality_audit.xml file in this file as well.

    What other configurations do I need to alter to get this working?
    Last edited by cudaguy; Jun 3rd, 2009, 03:49 PM. Reason: grammer
Working...
X