Question

I can't find any example of jcabi-aspects in a Play! application.

Here's my Build.scala file

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

  val appName         = "test-for-fun"
  val appVersion      = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    // Add your project dependencies here,
    "com.jcabi" % "jcabi-aspects" % "1.0-SNAPSHOT",
    "org.aspectj" % "aspectjrt" % "1.6.12" % "runtime",
    javaCore,
    javaJdbc,
    javaEbean
  )

  val main = play.Project(appName, appVersion, appDependencies).settings(
    resolvers += "oss.sonatype.org" at "https://oss.sonatype.org/content/repositories/snapshots/" 
  )

}

And here's how I call @Loggable

package controllers;

import play.*;
import play.mvc.*;
import com.jcabi.aspects.Loggable;

import views.html.*;

public class Application extends Controller {
    @Loggable(Loggable.INFO)
    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }

}

If you can provide any tips you are welcome.

My main goal is to perform logging using AOP so if you know an other way using Play! you are welcome as well.

Thanks

Was it helpful?

Solution

I was actually looking into the same thing today and got it working with my scala project (which uses SBT, though I've been running stuff through intellij). I think you're going to have to do a few things:

  1. As mentioned here, you'll want to add a -javaagent:~/path-to/aspectjweaver.jar for whatever java command you are running. See here for an example of how to add the javaagent flag for a play project.

  2. Include some extra dependencies:

    libraryDependencies += "org.aspectj" % "aspectjweaver" % "1.7.2"

    libraryDependencies += "org.aspectj" % "aspectjrt" % "1.7.2"

    libraryDependencies += "com.jcabi" % "jcabi-aspects" % "0.8"

    libraryDependencies += "com.jcabi" % "jcabi-log" % "0.8"

  3. Create a META-INF/aop.xml in your resources folder for your project. This is what defines the runtime weaving for aspectj. See this gist for an example.

  4. Copy the MethodLogger class from jcabi-aspects v0.8 (and Mnemos.java and NamedThreads.java) into your com.yourcompany.yourpackage. The reason for this (as far as I can tell) is that the jcabi packages are compiled in a special way, and we want to do run-time weaving instead, so this gets around that. Edit: perhaps this is due to me using Java 7 and the plugins being compiled in Java 6--I see some warnings when building in SBT.

I'd be happy to hear if someone has a better way, but this at least seems to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top