【问题标题】:How to solve error: package org.springframework.context.annotation does not exist import org.springframework.context.annotation.Bean?如何解决错误:包 org.springframework.context.annotation 不存在导入 org.springframework.context.annotation.Bean?
【发布时间】:2021-12-23 10:40:50
【问题描述】:

将 gradle 包装器更新到 7.3.2 版后,在尝试构建我的 spring boot 项目时出现以下错误:

error: package org.springframework.context.annotation does not exist
import org.springframework.context.annotation.Bean;

为什么会这样?

更新: 找到了问题,但还不知道如何解决。 我的项目中有多个模块。我有一个由其他模块使用的“通用”模块。旧的 gradle 版本将公共项目的库添加到其他模块的 compileClasspath 中,而新版本仅将它们添加到 runtimeClasspath 中。

依赖项过去被声明为编译,现在使用 gradle 7+ 我不得不将它们更改为实现。

有什么建议吗?

在其他模块中:

    dependencies {
        implementation project(':common')
...
    }

在通用模块中:

dependencies {
    implementation spring.web
...
}

【问题讨论】:

标签: java spring spring-boot gradle build.gradle


【解决方案1】:

如果您使用 java-library 而不是 java 插件,我认为您的传递性有问题

plugins {
   id 'java-library'
}

https://docs.gradle.org/current/userguide/java_library_plugin.html#java_library_plugin
那么你可以使用 api 而不是 compile 这可以帮助
你可以看到一个很好的解释What's the difference between implementation, api and compile in Gradle?

【讨论】:

  • 使用 id 'java-library' 并将依赖项声明为 api 有所帮助。
猜你喜欢
  • 2022-11-11
  • 2017-03-19
  • 2019-04-26
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 2020-11-01
相关资源
最近更新 更多