2

我正在尝试使用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
4

3 回答 3

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 中。

于 2017-08-27T12:18:49.810 回答
0

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

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

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

于 2015-06-21T16:10:14.663 回答
0

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

AspectjKeys.inputs in Aspectj <+= compiledClasses

products in Compile <<= products in Aspectj

products in Runtime <<= products in Compile

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

于 2015-06-25T09:06:46.220 回答