Best view in Firefox and Chrome

AspectJ Load-time Weaving with Spring 2.5

Monday, November 9, 2009
Convert Article to PDFPrint ArticleEmail Article to FrinedBookmark this Article



In the software programing the new methodology called AOP (Aspect-Oriented programming) provides separation of crosscutting concerns by introducing a new unit of modularization—an aspect—that crosscuts other modules. AspectJ has become the widely-used de-facto standard for AOP by emphasizing simplicity and usability for end users.

The AspectJ weaver play a vital role in the AOP concept with AspectJ. It takes class files as input and produces class files as output. But it will weave the AOP crosscuting concepts with he class files. The weaving process itself can take place at one of three different times: Compile-time, Post-compile time, and Load-time. And also the class files produced by the weaving process are the same regardless of the approach chosen to enable the AOP.

The concept Compile-time weaving and the Post-compile time weaving are some thing straight forward. But the Load time weaving is different from other two, and it will weave the classfile during the loadtime of an application.

Now how can we do with that AspectJ. Here I have given an example to do the Load time weaving with Spring.

Note : Spring is also proving a different AOP framework- Spring AOP. But I'm using the AspectJ as AOP, and Spring as the application Framework. Spring 2.5 added support to make it easier to use AspectJ's LTW. You can activate it by specific classloaders using the new context namespace by adding this, , to your XML configuration.

Load-time weaving (LTW) is - binary weaving defered until the point that a class loader loads a class file and defines the class to the JVM. To enable the LTW we required to specify one or more "weaving class loaders" which can provide either explicitly by the run-time environment or enabled through a "weaving agent".
-javaagent:/path/to/jar/spring-agent.jar
If you use the Maven Project management Tool, then you can give this argument by specifying the following lines in the pom.xml file, under the build tree.

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <forkMode>once</forkMode>
     <argLine>-javaagent:${settings.localRepository}/org/springframework/spring-agent/2.5.4/spring-agent-2.5.4.jar</argLine>
     <useSystemClassloader>true</useSystemClassloader>

     <systemProperties>
      <property>
       <name>aj.weaving.verbose</name>
       <value>true</value>
      </property>
     </systemProperties>
     <executions>
      <execution>
       <goals>
        <goal>test-compile</goal>
       </goals>
      </execution>
     </executions>
    </configuration>
   </plugin>



Now look at the application context file.
<?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"
 xmlns:tx="http://www.springframework.org/schema/tx"
 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
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


 <context:load-time-weaver aspectj-weaving="on" />

</beans>


Now the time to define the AspectJ aj file which have the ascpects to do the weaving at the load time. That AspectJ class must be and abstract class that will need to extend during the load time. Why I'm using an abstract class approach is it will give the flexibility to enable any kind of pointcuts at the loadtime. We will create a extended class for that Abstract AspectJ class when we load the application. Then we can easily include and exclude the classes which need to weave and which are no need.

public abstract aspect AbstractAspect {

 abstract pointcut scopeForPointcut();

 after() : scopeForPointcut() {
  System.out.println("\t\t Inside the Aspectj scopeForPointcut Method");
 }
}

After that, We have to define the aop.xml file inside the META-INF, which you can have it in your classpath. Look at the entry of the aop.xml file.
<!DOCTYPE aspectj PUBLIC
        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>

 <weaver options="-verbose">
  <!-- only weave classes in this package -->

  <include within="com.breeze.shayanth.examples.Test..*" />

  <!-- Exclude weave classes in this package -->
  <exclude within="com.breeze.shayanth.examples.aspectJ..*" />
  <exclude within="com.breeze.shayanth.examples.TestPackege..*" />
 </weaver>
 <aspects>
  <concrete-aspect
   name="com.breeze.shayanth.examples.aspectJ.__Implementaion__ConcuriteAbstractAspect"
   extends="com.breeze.shayanth.examples.aspectJ.AbstractAspect">
   <pointcut name="scopeForPointcut" expression="execution(* *.*(..))" />
  </concrete-aspect>
 </aspects>

</aspectj>


That's it. Now run the project. If you are using maven, use mvn clean install. You can easily understand the AspectJ Load-time Weaving with Spring.

Download Sources For this Example - Project

Help : How to run This Project. This project is using Maven so you can simply run this by,
mvn eclipse:clean eclipse:eclipse - to create a Eclipse Project
mvn clean install - to run this project.



Enjoy

Read these Articles :

1 comments:

Anonymous said...

ha, I will test my thought, your post bring me some good ideas, it's truly awesome, thanks.

- Norman

Why Breeze

This is a new technological rhythm in the web. It blooms like a Breeze in your technological face with more than 75 technoligical atricles and guidences.

Contact Me
Contact Me Send an Email Facebook Account LinkedIn profile Twitter/Shayanth
Share And Save
Social Share with Twitter Facebook Share Add to Google Save on Delicious
Get Updates

The RSS will facilitate you to get the updates from Breeze. Click Here to redirect to the RSS link.

Designed by Posicionamiento Web | Bloggerized by GosuBlogger | Blue Business Blogger | Customized by SHAUOM