
AspectJ with Spring applications.
May 21, 2010Tracing and auditing is one of the most common ways to monitor an application.
If your application is Spring-based, you can use Spring AOP for auditing and monitoring(via interceptors). But what if you want to add custom annotations to each audited method and have an access to these custom annotation at run time? In order to do that you need Load Time Weaving (LTW) with AspectJ in the Spring framework.
Here is how to add AspectJ auditing to your Spring-based web application.
Note : In order to use ASPECTJ LTW with Spring based web application, you have to use release Spring 2.5.0RC2 or later.
- 1. Create custom annotation:
package com.audit.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value=ElementType.METHOD)
@Retention(value=RetentionPolicy.RUNTIME)
public @interface AppAudit {
public String stepName();
public String stepDescription();
}
- 2. Create an Aspect, which will include around advice and pointcut.
package com.audit.aspects;
@Aspect
public class AuditAspect {
@Around("methodsToBeProfiled(appAudit)")
public Object profile(ProceedingJoinPoint pjp, AppAudit appAudit)
throws Throwable {
….. //<-- you can access your annotation here
return pjp.proceed();
}
@Pointcut(
"execution(@com.audit.annotations.AppAudit* *(..)) &&
@annotation(appAudit)")
public void methodsToBeProfiled(AppAudit appAudit){}
}
Please note, that ‘methodsToBeProfiled’ pointcut uses custom annotation AppAudit for selection. It selects all join points (methods) which are annotated with AppAudit.
The following is an example of a join point method, which will be selected by this pointcut:
@AppAudit(stepName="step name”, stepDescription="step description”)
public List getProjects(Long countryId) {
.......
}
- 3. Create weaverContext.xml config file and save it in your applications WEB-INF directory:
<?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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<context:load-time-weaver/>
</beans>
- 4. Add weaverContext.xml to contextConfigLocation in web.xml:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/application-context.xml <-- your application context /WEB-INF/weaverContext.xml </param-value> </context-param>
- 5. Modify your application context file ( in our example application-context.xml) by adding AspectJ related context:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> ….. <bean class= "org.springframework.aop.aspectj.annotation. AnnotationAwareAspectJAutoProxyCreator"/> <bean class= "org.springframework.beans.factory.annotation. RequiredAnnotationBeanPostProcessor"/> <!-- AspectJ related context --> <context:spring-configured/> <context:annotation-config/> <aop:aspectj-autoproxy/> <bean id="auditAspect" class="com.audit.aspects.AuditAspect"/> …… </beans>
- 6. Prepare Tomcat to use AspectJ LTW:
- 6.1 Create aop.xml file and copy it to your application META-INF directory:
<!DOCTYPE aspectj PUBLIC
“-//AspectJ//DTD//EN” “http://www.eclipse.org/aspectj/dtd/aspectj.dtd”><aspectj>
<weaver options=”-showWeaveInfo
-XmessageHandlerClass:org.springframework.aop.aspectj.
AspectJWeaverMessageHandler”>
<!– only weave classes in our application-specific packages –>
<!–package which will be audited–>
<include within=”com.myorg.myapp*”/>
</weaver><aspects>
<!– weave in just this aspect –>
<aspect name=
“com.audit.aspects.AuditAspect”/><–include your aspect
</aspects>
</aspectj>
- 6.2 Create context.xml file and copy it to your application META-INF directory:
<Context path=”/mywebapp” reloadable=”false”> <– your application root
<Loader loaderClass=
“org.springframework.instrument.classloading.tomcat.
TomcatInstrumentableClassLoader”
useSystemClassLoaderAsParent=”false”/>
</Context>
- 6.3 Add spring-tomcat-weaver.jar to $CATALINA_HOME/lib directory.
Tomcat directory structure:
$CATALINA_HOME/lib/
|-annotations-api.jar
|-spring-tomcat-weaver.jar
|-.......
$CATALINA_HOME/webapps/
|-mywebapp <-- your web application
|-META-INF/
|-aop.xml
|-context.xml
|-WEB-INF/
|-weaverContext.xml
|-.........
|-lib/
|-aspectjrt.jar
|-aspectjweaver.jar
|-spring-aop.jar
|-spring-aspects.jar
|-........