What is LTW?
Load-time weaving (LTW) is simply binary weaving defered until the point that a class loader loads a class file and defines the class to the JVM. To support this, one or more "weaving class loaders", either provided explicitly by the run-time environment or enabled through a "weaving agent" are required.Following the below mentioned steps to enabled AspectJ LTW in any web based application.
Step 1: Create Aspect class
Create ProfilingAspect like below
package com.test.peek.agent;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class ProfilingAspect {
private static Logger logger = LoggerFactory.getLogger(ProfilingAspect.class);
@Around("methodsToBeProfiled()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
try {
logger.info("****************************************************Going to call the method.**************************************");
return pjp.proceed();
} finally {
logger.info("****************************************************Method Execution completed.**************************************");
}
}
@Pointcut("execution(public * com.test.peek..*.*(..))")
public void methodsToBeProfiled(){}
}
Step 2: Creat aop.xml in META-INF folder
Create aop.xml in META-INF folder with the following content.
<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<exclude within="*..*CGLIB*" />
<!-- only weave classes in our application-specific packages -->
<include within="com.test.peek.web.service.SearchService"/>
<!-- Package name of Aspect class -->
<include within="com.test.peek.agent.*"/>
<include within="com.test.peek.agent.ProfilingAspect"/>
<include within="com.test.peek.agent.ProfilingAspect"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.test.peek.agent.ProfilingAspect"/>
</aspects>
</aspectj>
Step 3: Passing spring-instrument jar path as a VM -javaagent argument
The below mentioned argument should be passed as an VM argument while starting tomcat server.-javaagent:/Users/test.m2/repository/org/aspectj/aspectjweaver/1.7.2/aspectjweaver-1.7.2.jar
Step 4: Maven Dependency
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.0</version>
</dependency>
Just follow the above mentioned step to enable LTW in any web based application.
For more details refer : http://www.eclipse.org/aspectj/doc/released/devguide/ltw-configuration.html.
No comments:
Post a Comment