没有Spring Context且带有OpenJDK 11的AspectJ 1.9.4无法在Spark路由上作为依赖模块工作

时间:2019-06-03 14:40:28

标签: java apache-spark aspect

我正在尝试使用AspectJ创建自定义的加载时注释,在没有Spring Context的情况下打开JDK11。它在模块中可以正常工作,并且注释在类加载时进行编织,而方面在运行时执行。没问题,但是当AspectJ实现模块时,它添加为对具有火花路由的另一个模块的依赖。 AspectJ和注释未处理。我是否缺少任何配置?

父POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>de.scrum-master</groupId>
  <artifactId>aspectj-ltw-test-multi-module</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>11</java.source-target.version>
    <aspectj.version>1.9.4</aspectj.version>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
            <source>${java.source-target.version}</source>
            <target>${java.source-target.version}</target>
            <!-- IMPORTANT -->
            <useIncrementalCompilation>false</useIncrementalCompilation>
          </configuration>
        </plugin>
        <plugin>
          <groupId>com.nickwongdev</groupId>
          <artifactId>aspectj-maven-plugin</artifactId>
          <version>1.12.1</version>
          <configuration>
            <!--<showWeaveInfo>true</showWeaveInfo>-->
            <source>${java.source-target.version}</source>
            <target>${java.source-target.version}</target>
            <Xlint>ignore</Xlint>
            <complianceLevel>${java.source-target.version}</complianceLevel>
            <encoding>${project.build.sourceEncoding}</encoding>
            <!--<verbose>true</verbose>-->
            <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
          </configuration>
          <executions>
            <execution>
              <!-- IMPORTANT -->
              <phase>process-sources</phase>
              <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
          <dependencies>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjtools</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjweaver</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
          </dependencies>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.9</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
      </dependency>
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
      </dependency>
      <dependency>
        <groupId>de.scrum-master</groupId>
        <artifactId>aspect</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <modules>
    <module>aspect</module>
    <module>application</module>
  </modules>

</project>

方面模块:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>de.scrum-master</groupId>
    <artifactId>aspectj-ltw-test-multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>de.scrum-master</groupId>
  <artifactId>aspect</artifactId>
  <version>1.0-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <groupId>com.nickwongdev</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
    </dependency>
  </dependencies>
</project>
<aspectj>
  <aspects>
    <aspect name="de.scrum_master.aspect.CounterAspect"/>
  </aspects>
  <weaver options="-verbose">
    <!-- weave anything -->
    <include within="*"/>
  </weaver>
</aspectj>
package de.scrum_master.aspect;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Documented
@Inherited
@Target(METHOD)
@Retention(RUNTIME)
public @interface Counter {
  String name() default "";
}
package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class CounterAspect {
  @Around("execution(* *.*(..)) && @annotation(counter)")
  public void myBeforeLogger(ProceedingJoinPoint joinPoint, Counter counter) {
    System.out.println(joinPoint + " -> " + counter.name());
  }
}

应用程序模块:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>de.scrum-master</groupId>
    <artifactId>aspectj-ltw-test-multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>de.scrum-master</groupId>
  <artifactId>application</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>de.scrum-master</groupId>
      <artifactId>aspect</artifactId>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
     <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.9.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <argLine>
            -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
          </argLine>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
package de.scrum_master.app;

import de.scrum_master.aspect.Counter;

public class MyCounter {
  @Counter(name = "call_count")
  public void count() {}
}
package de.scrum_master.app;

import spark.Spark;
import static spark.Spark.get;

public class CounterApp {

 public static void main(String[] args) {
    get("/counter", (req, res) -> {
         new MyCounter().count();
         return "I ALREADY CALLED COUNTER ASPECT ON METHOD:MyCounter().count()";
    });
    System.out.println("Application starting on port :"+ Spark.port());

 }

}

注意::如果我在URL下方调用,则应处理我的注释。但这不是!

http://localhost:4567/counter

1 个答案:

答案 0 :(得分:0)

好吧,首先,您忘记将Spark Core添加到应用程序模块的依赖项中,而是仅在父POM的依赖项管理部分中定义了版本。否则由于未知的导入,Maven项目甚至将无法编译。

此外,我在对您的first question的评论中告诉您的内容同样适用:

  

就像通过测试一样,您可以通过命令行附加Java代理。

作为示例,我向您的应用程序POM中添加了Exec Maven插件,并将其配置为运行示例主类:

<!-- (...) -->

  <properties>
    <main-class>de.scrum_master.app.CounterApp</main-class>
  </properties>

<!-- (...) -->

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>
              -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
            </argument>
            <argument>-classpath</argument>
            <!-- automatically creates the classpath using all project dependencies, also adding the project build directory -->
            <classpath/>
            <argument>${main-class}</argument>
          </arguments>
        </configuration>
      </plugin>

<!-- (...) -->

然后您运行构建并随后运行应用程序:

$ mvn clean install
(...)

$ cd application

$ mvn exec:exec
(...)
[INFO] --------------------< de.scrum-master:application >---------------------
[INFO] Building application 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.4.0:exec (default-cli) @ application ---
[AppClassLoader@2aae9190] info AspectJ Weaver Version 1.9.4 built on Friday May 10, 2019 at 08:43:10 PDT
[AppClassLoader@2aae9190] info register classloader jdk.internal.loader.ClassLoaders$AppClassLoader@2aae9190
[AppClassLoader@2aae9190] info using configuration file:/C:/Users/alexa/.m2/repository/de/scrum-master/aspect/1.0-SNAPSHOT/aspect-1.0-SNAPSHOT.jar!/META-INF/aop.xml
[AppClassLoader@2aae9190] info register aspect de.scrum_master.aspect.CounterAspect
(...)
Application starting on port :4567
[AppClassLoader@2aae9190] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
(...)
[AppClassLoader@2aae9190] info processing reweavable type de.scrum_master.aspect.CounterAspect: de\scrum_master\aspect\CounterAspect.aj
[AppClassLoader@2aae9190] info successfully verified type de.scrum_master.aspect.CounterAspect exists.  Originates from de\scrum_master\aspect\CounterAspect.aj

然后调用您的URL http://localhost:4567/counter并检查正在运行的服务器的控制台输出:

execution(void de.scrum_master.app.MyCounter.count()) -> call_count

我为上一个问题添加到存储库中的完整提交是here