我已经把MCVE代码放在GitHub上-https://github.com/ravitechy/multi-module-project/tree/main
我的Java项目中有两个模块(构建在Gradle上)-app
,它依赖于commons
。commons
包含Aspect类BasicCommonAspect
,定义如下
package org.example.commons.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import javax.annotation.PostConstruct;
@Aspect
@EnableAspectJAutoProxy
public class BasicCommonAspect {
@Before("@annotation(bca) && execution(* *(..))")
public void executeBeforeForCommonAnnotation(JoinPoint joinPoint, BasicCommonAnnotation bca) {
System.out.println("BasicCommonAnnotation detected");
}
}
字符串commons
还包含如下注解BasicCommonAnnotation
package org.example.commons.aspect;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BasicCommonAnnotation {
}
型app
包含AppBackground
,并有一个用BasicCommonAnnotation
注解的方法run
package org.example;
import org.example.commons.aspect.BasicCommonAnnotation;
public class AppBackground {
@BasicCommonAnnotation
public void run() {
System.out.println("Running in background");
}
}
型app
模块中有一个名为RunApp
的驱动程序类,它调用AppBackground
中的run
方法
package org.example;
public class RunApp {
public static void main(String[] args) {
final AppBackground background = new AppBackground();
background.run();
}
}
型
我使用编译后编织(使用Gradle插件io.freefair.aspectj.post-compile-weaving
),因为我需要使用Project Lombok。
当我运行RunApp
类时,我的目标是看到方法BasicCommonAspect.executeBeforeForCommonAnnotation()
按照定义的通知执行。
从我的初步研究中,我知道我需要将外部模块作为方面库包含在apps
模块中。我在这里找到了完全相同问题的Stack Overflow答案-AspectJ - Aspect from external JAR,但它适用于Maven构建。我正在为我的Gradle项目寻找完全相似的配置。
我尝试使用inpath
,就像这里建议的https://github.com/freefair/gradle-plugins/issues/450一样,但是我现在开始遇到不同的构建错误,如下所示。
Not skipping AJC (inpath is set)
Starting process 'command 'D:\OpenJdk17\bin\java.exe''. Working directory: D:\intellij-idea workspace\multi-module-project\app Command: D:\OpenJdk17\bin\java.exe -Dfile.encoding=windows-1252 -Duser.country=IN -Duser.language=en -Duser.variant -cp C:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.aspectj\aspectjtools\1.9.8\caf8b7f9023d93af2ed2e6e8bf023a800e9d1323\aspectjtools-1.9.8.jar org.aspectj.tools.ajc.Main -argfile D:\intellij-idea workspace\multi-module-project\app\build\tmp\compileJava\ajc.options
Successfully started process 'command 'D:\OpenJdk17\bin\java.exe''
←[0K
←[0K
←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [18s]←[m←[35D←[1B←[1m> :app:compileJava←[m←[18D←[1B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [19s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [20s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [21s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [22s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [23s]←[m←[35D←[2B←[2AC:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\2.7.18\9cf147c6ca274c75b32556acdcba5a1de081ebcd\spring-boot-autoconfigure-2.7.18.jar [error] can't determine implemented interfaces of missing type org.springframework.web.server.WebExceptionHandler
when weaving type org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
[Xlint:cantFindType]
(no source information available)
[Xlint:cantFindType]
←[0K
←[0K
←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [23s]←[m←[35D←[1B←[1m> :app:compileJava←[m←[18D←[1B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [24s]←[m←[35D←[2B←[2AC:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.7.18\f6dbdd8da7c2bded63dff9b1f48d01a4923f20a0\spring-boot-2.7.18.jar [error] can't determine implemented interfaces of missing type javax.servlet.Filter
when weaving type org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
[Xlint:cantFindType]
(no source information available)
[Xlint:cantFindType]
C:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.7.18\f6dbdd8da7c2bded63dff9b1f48d01a4923f20a0\spring-boot-2.7.18.jar [error] can't determine implemented interfaces of missing type io.undertow.server.HttpHandler
when weaving type org.springframework.boot.web.embedded.undertow.UndertowWebServer$CloseableHttpHandlerFactory$1
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
[Xlint:cantFindType]
(no source information available)
[Xlint:cantFindType]
C:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.7.18\f6dbdd8da7c2bded63dff9b1f48d01a4923f20a0\spring-boot-2.7.18.jar [error] can't determine implemented interfaces of missing type org.springframework.web.server.WebFilter
when weaving type org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
[Xlint:cantFindType]
(no source information available)
[Xlint:cantFindType]
型
因此,我的理解是,为了克服这个问题,我只需要在inpath
中包含特定包中的方面类,但是我似乎找不到这样做的配置。
请帮助我提供必要的配置,以包括来自外部JAR/模块的方面,只从特定的包。apps
模块中的build.gradle
文件
plugins {
id 'java'
id 'java-library'
id 'io.freefair.aspectj.post-compile-weaving' version "${aspectjVersion}"
}
java.sourceCompatibility = JavaVersion.VERSION_17
java.targetCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
// api project(":commons")
inpath project(":commons")
api libs.spring.boot.starter
annotationProcessor libs.lombok
implementation libs.aspectjrt
}
型commons
模块中的build.gradle
plugins {
id 'java'
id 'java-library'
id 'io.freefair.aspectj.post-compile-weaving' version "${aspectjVersion}"
}
java.sourceCompatibility = JavaVersion.VERSION_17
java.targetCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
api libs.spring.boot.starter
annotationProcessor libs.lombok
implementation libs.aspectjrt
}
型root
中的settings.gradle
rootProject.name = 'multi-module-project'
include('app')
include('commons')
dependencyResolutionManagement {
versionCatalogs {
libs {
version('spring-boot-ver', '2.7.18')
library('lombok', 'org.projectlombok:lombok:1.18.30')
library('spring-boot-starter', 'org.springframework.boot', 'spring-boot-starter').versionRef('spring-boot-ver')
library('aspectjrt','org.aspectj:aspectjrt:1.9.8')
}
testLibs {
library('junit-jupiter-api','org.junit.jupiter:junit-jupiter-api:5.8.1')
library('junit-jupiter-engine', 'org.junit.jupiter:junit-jupiter-engine:5.8.1')
}
}
}
型root
中的build.gradle
rootProject.ext {
projectGroup = 'org.example'
projectVersion = '1.0.0-SNAPSHOT'
aspectjVersion = '8.4'
}
型
P.S:如果所有的类、注解和方面都在一个模块中,那么一切都能按预期工作。
1条答案
按热度按时间bvjveswy1#
问题是你的应用程序模块中的
inpath project(":commons")
。如果你想将方面编织到第三方模块中,那么应该使用 *inpath *。后者应该在 inpath 上。但是在这种情况下,你只是想告诉插件在哪里找到方面来编织到当前模块中。然而,方面库属于 aspectpath。因此,您需要将行改为aspect project(":commons")
。这相当于将<aspectLibrary/>
从my answer改为您已经找到的相应Maven问题我已经为你的项目创建了一个pull request,修复了这个问题并改进了一些其他的东西。
现在,您可以构建并运行应用程序了。控制台日志显示:
字符串
也就是说,该方面按预期被触发。