Advanced AspectJ part V : integration with Spring

In this article we’ll see how to integrate your own aspects with Spring.

First we focus on load-time weaving using Spring’s dedicated AspectJ agents. Then we’ll see how to let Spring inject beans into your aspects

The below configuration is valid for Spring 3.0.5, Tomcat 6 & AspectJ 1.6.12

 

I Load-time weaving with Spring

A Spring configuration

To enable load-time weaving, we must add the <context:load-time-weaver> tag in the Spring XML configuration

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
	xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd&quot;&gt;
	...
	...
	&lt;context:load-time-weaver 
		weaver-class=&quot;org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver&quot;
		aspectj-weaving=&quot;on&quot;/&gt;
	...
	...
&lt;/beans&gt;

The weaver class org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver is applicable to Tomcat 6. If you are using another application server or servlet container, please refer to Spring official recommendation (table 7.1) here : Weaver Class configuration

You need to add extra Maven dependencies to your project’ pom.xml to get this class:

	&lt;dependency&gt;
  		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
  		&lt;artifactId&gt;spring-instrument&lt;/artifactId&gt;
  		&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
  		&lt;type&gt;jar&lt;/type&gt;
  		&lt;scope&gt;compile&lt;/scope&gt;
  	&lt;/dependency&gt;
	&lt;dependency&gt;
  		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
  		&lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
  		&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
  		&lt;type&gt;jar&lt;/type&gt;
  		&lt;scope&gt;compile&lt;/scope&gt;
  	&lt;/dependency&gt;

spring-instrument-3.0.5.RELEASE.jar contains an Instrumentation agent needed at runtime and spring-aspects-3.0.5.RELEASE.jar contains aspects for Spring’s:

  • @Transactional support
  • @Configurable support
  • JPA Exception translation support
  • @Async annotation for scheduling support

 

B Tomcat configuration

Spring is shipped with a special AspectJ agent dedicated to Tomcat server. First you need to download the jar by adding this Maven dependency:

	&lt;dependency&gt;
  		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
  		&lt;artifactId&gt;spring-instrument-tomcat&lt;/artifactId&gt;
  		&lt;version&gt;3.0.5.RELEASE&lt;/version&gt;
  		&lt;type&gt;jar&lt;/type&gt;
  		&lt;scope&gt;compile&lt;/scope&gt;
  	&lt;/dependency&gt;

Then put the spring-instrument-tomcat-3.0.5.RELEASE.jar file in the <Tomcat_Install>/lib folder.

Last but not least, add a custom class loader in Tomcat’ configuration for your web app:

      	 &lt;Context docBase=&quot;myAspectJApp&quot; 
      	       	 path=&quot;/myAspectJApp&quot; reloadable=&quot;true&quot;
      	       	 source=&quot;org.eclipse.jst.jee.server:myAspectJApp&quot;&gt;
      	       	 &lt;Loader loaderClass=&quot;org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader&quot;/&gt;
      	 &lt;/Context&gt;

There is still one last configuration step to go.
 

C aop.xml

You need to create an aop.xml file and at it to the META-INF folder of your web app.

A sample aop.xml file:

&lt;!DOCTYPE aspectj PUBLIC &quot;-//AspectJ//DTD//EN&quot; &quot;http://www.eclipse.org/aspectj/dtd/aspectj.dtd&quot;&gt;
&lt;aspectj&gt;
    &lt;weaver options=&quot;-verbose -showWeaveInfo -Xreweavable&quot;&gt;
		&lt;include within=&quot;com.myApp..*&quot;/&gt;
    &lt;/weaver&gt;

    &lt;aspects&gt;
		&lt;concrete-aspect name=&quot;com.myApp.aspect.AspectPrecedence&quot;
precedence=&quot;com.myApp.aspect.metric.MetricsAspect,com.myApp.aspect.exception.ExceptionCatcherAspect&quot; /&gt;
				
		&lt;aspect name=&quot;com.myApp.aspect.metric.MetricsAspect&quot;/&gt;
		&lt;aspect name=&quot;com.myApp.aspect.exception.ExceptionCatcherAspect&quot;/&gt;
    &lt;/aspects&gt;

  &lt;/aspectj&gt;

Inside the <weaver> tag we declare all target classes to be woven with aspects by AspectJ. In the example, we restrict the classes to our application package to avoid AspectJ runtime to scan all classes on the classpath (very slow).

Inside the <aspects> tag we list all the aspects needed for the weaving process. You can also declare a concrete aspect to manage aspect precedence at this place.

You need not declare in the <aspects> section all Spring standard aspects (for @Transactional, @Configurable…). They are already declared in another aop.xml file in the spring-aspects-3.0.5.RELEASE.jar

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!--
	AspectJ load-time weaving config file to install common Spring aspects.
--&gt;
&lt;aspectj&gt;

	&lt;!--
	&lt;weaver options=&quot;-showWeaveInfo&quot;/&gt;
	--&gt;

	&lt;aspects&gt;
		&lt;aspect name=&quot;org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect&quot;/&gt;
		&lt;aspect name=&quot;org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect&quot;/&gt;
		&lt;aspect name=&quot;org.springframework.transaction.aspectj.AnnotationTransactionAspect&quot;/&gt;
	&lt;/aspects&gt;

&lt;/aspectj&gt;

 

II Spring’ aspects integration

To perform some business logic, your aspects may need to get a reference to a Spring managed bean. For this we can rely on dependency injection. However since the aspect instanciation and lifecycle is not managed by Spring but by AspectJ runtime, how can dependency be injected ?

The aspectOf() method comes to the rescue. Refer to Advanced AspectJ Part I : Instanciation model, chapter II Accessing the instance for more details on it.

By calling aspectOf() we can get the reference of the aspect instance. Dependency injection can be achieved by passing this instance to Spring.

	&lt;bean class=&quot;com.myApp.aspect.metric.MetricsAspect&quot; factory-method=&quot;aspectOf&quot;&gt;
		&lt;property name=&quot;myDao&quot; ref=&quot;myDao&quot;/&gt;
		&lt;property name=&quot;myEntityManagerFactory&quot; ref=&quot;myEntityManagerFactory&quot;/&gt;
	&lt;/bean&gt;		

Of course this trick only works for aspect with singleton instanciation model. The other instanciation models need the target object as argument for aspectOf() (perthis, pertaget instanciations) or the control flow context (percflow, percflowbelow instantiations).

If your aspects are advising the same join points as Spring standard aspects (@Transactional for example) you can change your aspect precedence against Spring’ one.

&lt;!DOCTYPE aspectj PUBLIC &quot;-//AspectJ//DTD//EN&quot; &quot;http://www.eclipse.org/aspectj/dtd/aspectj.dtd&quot;&gt;
&lt;aspectj&gt;
    &lt;weaver options=&quot;-verbose -showWeaveInfo -Xreweavable&quot;&gt;
		&lt;include within=&quot;com.myApp..*&quot;/&gt;
    &lt;/weaver&gt;

    &lt;aspects&gt;
		&lt;concrete-aspect name=&quot;com.myApp.aspect.AspectPrecedence&quot;
precedence=&quot;com.myApp.aspect.metric.MetricsAspect,org.springframework.transaction.aspectj.AnnotationTransactionAspect&quot; /&gt;
				
		&lt;aspect name=&quot;com.myApp.aspect.metric.MetricsAspect&quot;/&gt;
    &lt;/aspects&gt;

  &lt;/aspectj&gt;

In the above example, the custom MetricsAspect aspect has been declared with higher precedence than Spring’ AnnotationTransactionAspect (@Transactional).

2 Comments

  1. Declan

    I personally really want to bookmark this specific article,
    “Advanced AspectJ part V : integration with
    Spring | DuyHai’s Java Blog” on my page. Will you care in the event I reallydo it? Thanks a lot ,Tonja

    Reply
    1. DuyHai DOAN

      Please do so, no prob

      Reply

Leave a Reply to Declan Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.