【问题标题】:AspectJ pointcuts in Scala using SBTScala 中使用 SBT 的 AspectJ 切入点
【发布时间】:2015-06-17 15:41:18
【问题描述】:

我正在尝试使用sbt-aspectj 插件在Scala 中使用AspectJ 切入点构建一个hello world 示例。在我的 plugins.sbt 中,我添加了插件:

addSbtPlugin("com.typesafe.sbt" % "sbt-aspectj" % "0.10.2")

在我的 build.sbt 中,我将其设置如下:

import com.typesafe.sbt.SbtAspectj._

aspectjSettings

/* project settings */

fork in run := true

javaOptions in run <++= AspectjKeys.weaverOptions in Aspectj

products in Compile <++= products in Aspectj

然后我创建了一个非常简单的 AspectJ 文件,围绕我的一个服务器类定义了一个切入点:

包 io.bigsense;

public aspect AspectLogger {

  private long start;

  pointcut conn() : call(* io.bigsense.server.TomcatServer.startServer(..));

  void around() : conn() {
    start = System.currentTimeMillis();
    Throwable t = null;
    try {
      proceed();
    }
    catch(Throwable _t) {
      t = _t;
    }
    long spent = System.currentTimeMillis() - start;
    System.out.println("Send time: " + spent);
    if (t != null) {
      throw new RuntimeException(t);
    }
  }

}

所以类和函数确实存在。但是每当我运行 sbt 时,我都会收到以下消息:

[info] Weaving 1 AspectJ source to /home/cassius/sensespace/BigSense/target/scala-2.10/aspectj/classes...
[warn] warning at /home/cassius/sensespace/BigSense/src/main/aspectj/AspectLogger.aj:11::0 advice defined in io.bigsense.AspectLogger has not been applied [Xlint:adviceDidNotMatch]

我查看了很多示例,我的切入点看起来是正确的。为什么它没有在运行时应用/编织到我的代码中?

编辑:添加 sbt 启动 java 的命令行参数:

/opt/oracle-jdk-bin-1.7.0.80/jre/bin/java
-javaagent:/home/cassius/.ivy2/cache/org.aspectj/aspectjweaver/jars/aspectjweaver-1.8.5.jar
-classpath
/home/cassius/sensespace/BigSense/target/scala-2.10/classes:/home/cassius/sensespace/BigSense/target/scala-2.10/aspectj/classes:/home/cassius/sensespace/BigSense/lib/ij.jar:/home/cassius/sensespace/BigSense/lib/eproperties-1.1.3.jar:/home/cassius/.sbt/boot/scala-2.10.4/lib/scala-library.jar:/home/cassius/.ivy2/cache/com.typesafe.play/twirl-api_2.10/jars/twirl-api_2.10-1.0.4.jar:/home/cassius/.ivy2/cache/org.apache.commons/commons-lang3/jars/commons-lang3-3.1.jar:/home/cassius/.ivy2/cache/org.aspectj/aspectjrt/jars/aspectjrt-1.8.5.jar:/home/cassius/.ivy2/cache/org.springframework/spring-beans/jars/spring-beans-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/org.springframework/spring-core/jars/spring-core-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/org.springframework/spring-asm/jars/spring-asm-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar:/home/cassius/.ivy2/cache/org.springframework/spring-context/jars/spring-context-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/org.springframework/spring-aop/jars/spring-aop-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/aopalliance/aopalliance/jars/aopalliance-1.0.jar:/home/cassius/.ivy2/cache/org.springframework/spring-expression/jars/spring-expression-3.0.5.RELEASE.jar:/home/cassius/.ivy2/cache/cglib/cglib/jars/cglib-2.2.2.jar:/home/cassius/.ivy2/cache/asm/asm/jars/asm-3.3.1.jar:/home/cassius/.ivy2/cache/commons-codec/commons-codec/jars/commons-codec-1.6.jar:/home/cassius/.ivy2/cache/commons-io/commons-io/jars/commons-io-2.4.jar:/home/cassius/.ivy2/cache/net.sourceforge.jtds/jtds/jars/jtds-1.2.4.jar:/home/cassius/.ivy2/cache/com.jolbox/bonecp/bundles/bonecp-0.7.1.RELEASE.jar:/home/cassius/.ivy2/cache/com.google.guava/guava/jars/guava-r08.jar:/home/cassius/.ivy2/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.12.jar:/home/cassius/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.7.12.jar:/home/cassius/.ivy2/cache/log4j/log4j/bundles/log4j-1.2.17.jar:/home/cassius/.ivy2/cache/org.scalaj/scalaj-collection_2.10/jars/scalaj-collection_2.10-1.5.jar:/home/cassius/.ivy2/cache/bouncycastle/bcprov-jdk15/jars/bcprov-jdk15-140.jar:/home/cassius/.ivy2/cache/org.postgresql/postgresql/jars/postgresql-9.4-1201-jdbc41.jar:/home/cassius/.ivy2/cache/org.postgis/postgis-jdbc/jars/postgis-jdbc-1.3.3.jar:/home/cassius/.ivy2/cache/org.postgis/postgis-stubs/jars/postgis-stubs-1.3.3.jar:/home/cassius/.ivy2/cache/postgresql/postgresql/jars/postgresql-8.3-603.jdbc4.jar:/home/cassius/.ivy2/cache/mysql/mysql-connector-java/jars/mysql-connector-java-5.1.30.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-server/jars/jetty-server-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/javax.servlet/javax.servlet-api/jars/javax.servlet-api-3.1.0.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-http/jars/jetty-http-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-util/jars/jetty-util-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-io/jars/jetty-io-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-servlet/jars/jetty-servlet-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-security/jars/jetty-security-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-webapp/jars/jetty-webapp-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.eclipse.jetty/jetty-xml/jars/jetty-xml-9.1.4.v20140401.jar:/home/cassius/.ivy2/cache/org.rogach/scallop_2.10/jars/scallop_2.10-0.9.5.jar:/home/cassius/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.10.3.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-core/jars/tomcat-embed-core-7.0.53.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-logging-juli/jars/tomcat-embed-logging-juli-7.0.53.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-jasper/jars/tomcat-embed-jasper-7.0.53.jar:/home/cassius/.ivy2/cache/org.apache.tomcat.embed/tomcat-embed-el/jars/tomcat-embed-el-7.0.53.jar:/home/cassius/.ivy2/cache/org.eclipse.jdt.core.compiler/ecj/jars/ecj-P20140317-1600.jar:/home/cassius/.ivy2/cache/org.apache.commons/commons-compress/jars/commons-compress-1.6.jar:/home/cassius/.ivy2/cache/org.tukaani/xz/jars/xz-1.4.jar
io.bigsense.server.BigSenseServer
-c
../bs-env/mssql-vm.conf

【问题讨论】:

    标签: scala sbt aspectj sbt-aspectj


    【解决方案1】:

    对于 sbt 0.13.16 现在是

    addSbtPlugin("com.lightbend.sbt" % "sbt-aspectj" % "0.11.0")
    

    在 plugins.sbt 和

    import com.lightbend.sbt.SbtAspectj._
    
    scalaVersion := "2.12.3"
    
    aspectjSettings
    
    aspectjInputs in Aspectj += (aspectjCompiledClasses in Aspectj).value
    
    products in Compile := (products in Aspectj).value
    
    products in Runtime := (products in Compile).value
    

    在 build.sbt 中。

    【讨论】:

      【解决方案2】:

      [Xlint:adviceDidNotMatch] 始终意味着 AspectJ 编织器/编译器在其 inpath 上看不到任何与您的切入点匹配的连接点。

      在这种情况下,这意味着没有找到调用* io.bigsense.server.TomcatServer.startServer(..) 的代码行。可能调用不是来自您自己的源代码,而是来自一些不是由 AspectJ 编织的第 3 方库。因此,您要么需要编织调用代码,要么对收到此警告不应该感到惊讶。 ;-)

      如果您认为我错了,请提供SSCCE 让我重现您的问题。

      【讨论】:

      • 该类存在。你可以在这里找到它:github.com/BigSense/BigSense/blob/master/src/main/scala/io/… 该类的正确切入点是什么?或者更好的是,获取该类中每个函数的通用切入点是什么?
      • 请仔细阅读我的回答。我没有说这个类不存在。我说过该类的调用者(您要拦截的那些)不在 AspectJ 的 inpath 上。再次,请提供 SSCCE。否则,我很难对你的问题的原因说更多的实质性内容。我保证,我可以通过 SSCCE。
      • 我的问题是关于 sbt-aspectj 插件的。这是我第一次使用 AspectJ(试图从 Spring AOP 迁移)。我添加了 SBT 运行的命令行。我看到 inpath 丢失了,所以我猜这是 sbt-aspectj 中的错误或缺少文档。我在其跟踪器上打开了一个问题。
      • 找到了答案。请参阅下面的答案。
      【解决方案3】:

      我查看了其他一些示例项目,发现我的build.sbt 缺少以下内容

      AspectjKeys.inputs in Aspectj <+= compiledClasses
      
      products in Compile <<= products in Aspectj
      
      products in Runtime <<= products in Compile
      

      这会将已编译的源代码放置在 AspectJ 的 inpath 中。

      【讨论】:

      • 正如我所说:inpath。 ;-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多