我的目标是在我的代码库中实现编译时编织,并发现了 AspectJ Maven 插件 (https://dev-aspectj.github.io/aspectj-maven-plugin/)。
aspectj.version = 1.9.21
maven-compiler.version = 3.11.0
java.version = 21
我做了以下修改:
在
dependencies
部分添加了以下代码:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
在
<plugins>
部分包含以下代码:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler.version}</version>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArguments>
<d>${project.build.directory}/unwoven-classes</d>
</compilerArguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.13.1</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectories>
<weaveDirectory>${project.build.directory}/unwoven-classes</weaveDirectory>
</weaveDirectories>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
由于Lombok和AspectJ之间的兼容性问题,我最初编译为
unwoven-classes
,然后再继续target/classes
。运行 mvn clean compile
会导致使用 [INFO] Join point...
成功构建。但是,在应用程序启动过程中,我遇到以下错误:
Caused by: org.aspectj.lang.NoAspectBoundException: Exception while initializing com.xyz.apirest.authorization.EnsureFromMonolith: java.lang.NoSuchMethodError: com.xyz.apirest.authorization.EnsureFromMonolith: method 'void <init>()' not found at com.xyz.apirest.authorization.EnsureFromMonolith.aspectOf(EnsureFromMonolith.java:1) at com.xyz.apirest.v1.internal.workflows.InternalDisbursementController.<clinit>(InternalDisbursementController.java:1) ... 124 common frames omitted Caused by: java.lang.NoSuchMethodError: com.xyz.apirest.authorization.EnsureFromMonolith: method 'void <init>()' not found at com.xyz.apirest.authorization.EnsureFromMonolith.ajc$postClinit(EnsureFromMonolith.java:1) at com.xyz.apirest.authorization.EnsureFromMonolith.<clinit>(EnsureFromMonolith.java:1) ... 125 common frames omitted
相关代码片段如下:
@Aspect
@Component
@RequiredArgsConstructor
public class EnsureFromMonolith {
private final AuthenticationProfile authenticationProfile;
private final XYZJwtAuthenticationConfig xyzJwtAuthenticationConfig;
protected final Logger logger = LoggerFactory.getLogger(EnsureFromMonolith.class);
@Around(value = "(execution(public * *(..)) && within(@com.xyz.apirest.authorization.RequireFromMonolith *)) || @annotation(com.xyz.apirest.authorization.RequireFromMonolith)")
public Object ensureFromMonolith(ProceedingJoinPoint joinPoint) throws Throwable {
if (authenticationProfile.getAid() == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} else if (!validateProfile(authenticationProfile, xyzJwtAuthenticationConfig.getMonolithAuthorizedClientId())) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return joinPoint.proceed();
}
public static boolean validateProfile(AuthenticationProfile authenticationProfile, String monolithClientId) {
return authenticationProfile.getAid().equals(monolithClientId);
}
}
我已经尝试了AspectJ 不适用于编译时编织和如何使用maven构建aspectj项目?中推荐的解决方案,但没有成功。
我的目标是让应用程序成功启动,AspectJ 按预期运行。`
由于使用了 Spring 的基于代理的 AOP,该代码不起作用,该 AOP 在将 @Component 注释与 Spring 的 AOP 设置一起使用时启用。这种类型的 AOP 仅支持 Spring bean 的公共方法。