gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:52:07 26 4
gpt4 key购买 nike

我正在尝试使用 AspectJ 创建自定义加载时注释,在没有 Spring Context 的情况下打开 JDK11。它在模块内运行良好,注释在类加载时编织,方面在运行时执行。没问题,但是当aspectJ实现模块添加为对具有spark路由的另一个模块的依赖项时。 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

最佳答案

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

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

You attach the Java agent via command line, just like you do for the test.

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

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

<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

关于java - 带有 OpenJDK 11 且没有 Spring Context 的 AspectJ 1.9.4 无法作为 Spark 路由上的依赖模块工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56429639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com