AspectJ loads but ignores class in Play Framework 2.2.x

I am having trouble getting my AspectJ aspect to work in the Play 2.2.2 framework. This seems to be a problem that other people have tried to solve (see here ), but I haven't been able to find an answer so far. This is what I am trying to do:

I have a java application for a game and I am trying to create @Trace annotation for method tracking and method tracking. So basically, I want a method that has been annotated with @Trace to record the name and input arguments and record the result on exit. I thought AOP with AspectJ would be an elegant way to do this, to avoid having to clutter my code with log.debug (...) calls.

Here's the aspect I came up with:

@Aspect
public class LoggingAspect {

    private static Logger log = LoggerFactory.getLogger(LoggingAspect.class);

    @Pointcut("@annotation(trace)")
    public void methodAnnotatedWithTrace(Trace trace) {}

    @Before("methodAnnotatedWithTrace(trace)")
    public void traceBefore(JoinPoint joinPoint, Trace trace) {
        log.debug("Entering - {} ", joinPoint.getSignature());
    }

    @AfterReturning("methodAnnotatedWithTrace(trace)")
    public void traceAfter(JoinPoint joinPoint, Trace trace) {
        log.debug("Exiting - {}", joinPoint.getSignature());
    }
}

      

In the build.sbt file, I added the following dependencies:

libraryDependencies += "org.aspectj" % "aspectjweaver" % "1.8.1"
libraryDependencies += "org.aspectj" % "aspectjrt"     % "1.8.1"

      

And I specified the following aop.xml file:

<aspectj>

  <aspects>
    <aspect name="aspects.LoggingAspect"/>
  </aspects>

  <weaver options="-verbose -XnoInline -showWeaveInfo">
    <include within="aspects.*"/>
    <include within="controllers.*"/>
  </weaver>

</aspectj>

      

I also added an aspect weaver to PLAY_OPTS:

set PLAY_OPTS=-javaagent:C:\\Users\\...\\play-2.2.2\\repository\\cache\\org.aspectj\\aspectjweaver\\jars\\aspectjweaver-1.8.1.jar

      

And I annotated my method with @Trace:

@Trace
public Result doSomething(String msg) { ... }

      

As a result, I get the following output when I run "play test":

[1@2a1ffa9f] info AspectJ Weaver Version 1.8.1 built on Saturday Jun 21, 2014 at 00:07:06 GMT
[1@2a1ffa9f] info register classloader sbt.classpath.ClasspathUtilities$$anon$1@2a1ffa9f
[1@2a1ffa9f] info using configuration /C:/Users/Stephanie/Documents/projects/zeitgenossen/target/scala-2.10/classes/META-INF/aop.xml
[1@2a1ffa9f] info register aspect aspects.LoggingAspect

      

It's all. I don't see a trace. It looks like the annotation is just being ignored.

What am I missing?

Thank you for your help!

+3


source to share


1 answer


Does your @Trace annotation have runtime preservation? If it is not, AspectJ will not see it in the class file when loading time.

BTW, @annotation(trace)

this is a very general form of annotation matching that tries to match annotations at any join point, not just about methods. Therefore, if you read or write an annotated field, or handle an exception whose type contains that annotation, they will all be matched. And if you call a method with an annotation, you get the advice twice, once for the "call" method and once for the "method" method of the method. You should probably restrict matching to the types of topics you are interested in that interest you:



execution(* *(..)) && @annotation(trace)

Now I am assuming you are only annotating methods, but digging around annotations can be costly, so if you limit it to just looking at the join points of the method execution, you can speed up the process.

0


source







All Articles