Scala 中使用 SBT 的 AspectJ 切入点
AspectJ pointcuts in Scala using SBT
我正在尝试使用 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 文件,定义了我的一个服务器周围的切入点 classes:
包 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);
}
}
}
所以 class 和函数确实存在。但是每当我 运行 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]
我看了很多例子,我的切入点看起来是正确的。为什么它在 运行 时没有被 applied/weaved 写入我的代码?
编辑:添加 sbt 正在启动的命令行参数 java with:
/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
[Xlint:adviceDidNotMatch]
总是意味着 AspectJ weaver/compiler 在其 inpath.
上看不到任何与切入点匹配的连接点
在这种情况下,这意味着没有找到调用 * io.bigsense.server.TomcatServer.startServer(..)
的代码行。调用可能不是来自您自己的源代码,而是来自不是由 AspectJ 编织的某些第 3 方库。因此,您要么需要编织调用代码,要么收到此警告并不奇怪。 ;-)
如果您认为我错了,请提供一个SSCCE让我重现您的问题。
我查看了其他一些示例项目,发现我的 build.sbt
中缺少以下内容
AspectjKeys.inputs in Aspectj <+= compiledClasses
products in Compile <<= products in Aspectj
products in Runtime <<= products in Compile
这会将编译后的源放在 AspectJ 的 inpath 中。
对于 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.
我正在尝试使用 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 文件,定义了我的一个服务器周围的切入点 classes:
包 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);
}
}
}
所以 class 和函数确实存在。但是每当我 运行 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]
我看了很多例子,我的切入点看起来是正确的。为什么它在 运行 时没有被 applied/weaved 写入我的代码?
编辑:添加 sbt 正在启动的命令行参数 java with:
/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
[Xlint:adviceDidNotMatch]
总是意味着 AspectJ weaver/compiler 在其 inpath.
在这种情况下,这意味着没有找到调用 * io.bigsense.server.TomcatServer.startServer(..)
的代码行。调用可能不是来自您自己的源代码,而是来自不是由 AspectJ 编织的某些第 3 方库。因此,您要么需要编织调用代码,要么收到此警告并不奇怪。 ;-)
如果您认为我错了,请提供一个SSCCE让我重现您的问题。
我查看了其他一些示例项目,发现我的 build.sbt
AspectjKeys.inputs in Aspectj <+= compiledClasses
products in Compile <<= products in Aspectj
products in Runtime <<= products in Compile
这会将编译后的源放在 AspectJ 的 inpath 中。
对于 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.