0

Implemented AOP using AspectJ without Spring. It works perfectly fine when running in Eclipse (Tomcat server) but not when run directly in Tomcat. Have added required dependencies in the pom but of no use. Not able to figure out the issue.

Aspect class:

@Aspect
public class FeatureAOP {
  private static final Logger LOG = LoggerFactory.getLogger(FeatureAOP.class);

  @Pointcut("execution(* x.y.z.rest.ModifiersFacadeWrapper.*(..)) && !execution(* x.y.z.rest.ModifiersFacadeWrapper.getUriInfo(..))")
  protected void pointCut() {
  }

  @Before("x.y.z.rest.aop.FeatureAOP.pointCut()  && this(mf) ")
  public void parseParams(JoinPoint jp, ModifiersFacadeWrapper mf) {
    LOG.info("Entered JoinPoint: {}", jp.getSignature());
    String feature = mf.getUriInfo().getPathParameters().get("feature").get(0);
    Feature featureEnum = Feature.get(feature);
    mf.setFeature(featureEnum);
    LOG.info("Feature set: {}", mf.getFeature());
  }
}

aop.xml:

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <weaver options="-verbose -showWeaveInfo -Xset:weaveJavaxPackages=true -debug">
        <include within="x.y.z"/>
    </weaver>
 <aspects>
  <aspect id="featureAspect" class="x.y.z.rest.aop.FeatureAOP" ></aspect>
 </aspects>
</aspectj>
  

Read in a few post to set javaagent in Tomcat to aspectjweaver lib. That also didn't help.

export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/home/sumit/Downloads/apache-tomcat-8.0.17/webapps/dpi-manager/WEB-INF/lib/aspectjweaver-1.8.5.jar"
sumsrv
  • 654
  • 3
  • 8
  • 25
  • There is a similar question here: http://stackoverflow.com/questions/10032092/how-to-configure-load-time-weaving-with-aspectj-and-tomcat and one possible answer there was to ensure the aop.xml file is in the right location. If you are getting no output at all related to weaving it must be either the agent not being set properly or the aop.xml being in the wrong place, rather than anything specifically wrong with the aspects. – Andy Clement May 25 '15 at 16:13
  • @AndyClement : Location of aop.xml was fine. Found the solution. I had to add aspectj-maven-plugin in maven. – sumsrv May 25 '15 at 17:22

1 Answers1

1

Found solution to this using maven. One needs to add aspectj-maven-plugin in pom.xml.

< plugin >
  < groupId > org.codehaus.mojo < /groupId>
    <artifactId>aspectj-maven-plugin</artifactId >
    < version > 1.4 < /version>
    <configuration>
        <source>1.7</source >
        <target > 1.7 < /target>
    </configuration >
  <executions >
    <execution >
      <goals>
      <goal>compile</goal >
      < /goals>
    </execution >
  < /executions>
  <dependencies>
    <dependency>
        <groupId>org.aspectj</groupId >
        < artifactId > aspectjrt < /artifactId>
        <version>${aspectj.version}</version >
    < /dependency>
    <dependency>
      <groupId>org.aspectj</groupId >
      < artifactId > aspectjtools < /artifactId>
      <version>${aspectj.version}</version >
    < /dependency>
  </dependencies >
< /plugin>

Example: https://github.com/mscharhag/blog-examples/blob/master/exception-translation/pom.xml

sumsrv
  • 654
  • 3
  • 8
  • 25