SpringBoot集成AspectJ时Gradle编译找不到AspectJ相关包问题求助
问题根源
你配置aspectjweaver依赖时使用了runtimeOnly作用域,该作用域的依赖仅会被加入运行时类路径,不会参与编译阶段的类路径解析,因此Java编译时无法找到AspectJ相关的包和类,触发编译报错。编辑器能正常识别补全是因为jdtls可能从全局依赖缓存等其他路径加载到了对应的类,和Gradle的构建类路径是相互独立的。
解决方案
有两种可行的修改方式:
方式1:直接修正现有依赖的作用域
将build.gradle的dependencies块中对应的依赖声明改为implementation作用域:
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' // 把runtimeOnly改为implementation,让依赖参与编译 implementation 'org.aspectj:aspectjweaver:1.9.7' }
方式2(更推荐):使用Spring官方AOP Starter
直接引入Spring Boot提供的AOP场景启动器,它会自动引入适配当前Spring Boot版本的AspectJ相关依赖以及AOP自动配置,无需手动指定版本,也能避免版本不兼容问题:
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' // 引入官方AOP starter,替代手动引入aspectjweaver implementation 'org.springframework.boot:spring-boot-starter-aop' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
修改完成后重新执行./gradlew bootRun即可正常编译运行。
内容的提问来源于stack exchange,提问作者wviana
相关产品推荐
相关产品推荐

