1

为什么我需要包含spring-context在我的新spring boot项目中才能工作?

我有这个在我的pom.xml

 <dependency> 
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
  </dependency> 

我可以在 spring-web 依赖项中看到依赖项 spring-context

如果我从 pom.xml 中删除 spring-context,则应用程序不会运行。

以下是我得到的错误:

Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nameOfController'
4

2 回答 2

0

您是否看到 spring-web 提供的 spring-context 或者真的只是依赖于它?您可能想帮自己一个忙,并使用实际上带有所有必需依赖项的 Spring Boot 启动器工件,并且不要将它们中的任何一个标记为“已提供”。

在您的情况下,它将是 spring-boot-starter-web。

于 2020-05-28T14:46:21.310 回答
0

当我们遍历 的编译依赖时spring-webspring-context则不包含在latest-version中。

https://frontbackend.com/maven/artifact/org.springframework/spring-web/5.2.6.RELEASE

虽然它存在于以前的版本中。

https://frontbackend.com/maven/artifact/org.springframework/spring-web/4.3.20.RELEASE

因为,我看不到您添加到 pom.xml 文件中的特定版本,所以我假设它正在获取最新版本,其中的依赖项spring-context不包含在spring-web.

此外,您提供的错误堆栈跟踪本质上非常少且通用,因此很难进一步调试您的问题,尽管我认为这是版本的问题。

您可以通过这种方式给出特定版本:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>
于 2020-05-28T16:45:25.670 回答