java - 为什么Spring-web没有在其POM中添加javax.javaee-api?

标签 java spring

在学习Spring框架时,发现一个奇怪的编译错误:

"can not find class:javax.servlet.ServletException".

搜索代码后,我在org.springframework.web.WebApplicationInitializer类中发现了一些代码,它使用了javax.servlet.ServletException:


package org.springframework.web;

import javax.servlet.ServletContext; //here
import javax.servlet.ServletException;//and here

public interface WebApplicationInitializer {
    void onStartup(ServletContext var1) throws ServletException;
}


但是,在其 POM 文件中,没有定义依赖项:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.2.1.RELEASE</version>
  <name>Spring Web</name>
  <description>Spring Web</description>
  <url>https://github.com/spring-projects/spring-framework</url>
  <organization>
    <name>Spring IO</name>
    <url>https://spring.io/projects/spring-framework</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <developers>
    <developer>
      <id>jhoeller</id>
      <name>Juergen Hoeller</name>
      <email>jhoeller@pivotal.io</email>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-framework</connection>
    <developerConnection>scm:git:git://github.com/spring-projects/spring-framework</developerConnection>
    <url>https://github.com/spring-projects/spring-framework</url>
  </scm>
  <issueManagement>
    <system>GitHub</system>
    <url>https://github.com/spring-projects/spring-framework/issues</url>
  </issueManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.1.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.1.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

我认为每个库都应该在其POM文件中写入其依赖项,以便Maven可以识别并自动下载依赖库。 为什么Spring-web省略了依赖?

最佳答案

Why Spring-web omit the dependency?

不知 Prop 体原因。您必须向开发人员询问完整的推理......

如果您将 spring-web 5.0.0 POM 文件与最新的 4.x.x POM 文件进行比较,您会发现依赖项发生了重大清理。很多cruft已删除,包括对 servlet api JAR 的任何依赖项。

如果您查看 spring-webmvc 5.0.0 的依赖项(显示 here )并向下滚动到提供的依赖项,您将看到存在对“javax.servlet » javax.servlet-api » 的依赖项4.0.0"。与 spring-web 5.0.0 进行对比(显示 here )。

请注意,spring-web(即您使用的依赖项)本身不能需要对 servlet-api 的依赖,否则它不会构建。

<小时/>

因此,这让我认为,如果您想获取可传递的 servlet-api 依赖项,也许您的项目需要依赖于 spring-webmvc 而不是 spring-web 。

(您正在阅读旧版本 Spring 的教科书或遵循教程或视频吗?如果是,我建议您在学习时更新教科书或使用旧版本的 Spring。)

<小时/>

回到你的问题:

Why Spring-web did not add javax.javaee-api in its POM?

原因之一是:因为它不需要它!

关于java - 为什么Spring-web没有在其POM中添加javax.javaee-api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59148970/

相关文章:

java - 为什么Spring Batch中没有调用分割流?

java - Spring Integration 使用 DSL 从 Unix 位置读取文件

java - 如何在 Spring Boot 应用程序上为数据库连接设置 TCP NODELAY

java - Maven 安装构建错误 - 依赖项路径

java - Android Studio 不编译

java - Java SE 中的连接池?

java - 如何在Java中创建类似于JFileChooser的东西?

java - 如何向参数为 Class< 的方法提供有效的类?扩展注释>?

java - 使用 @ParameterizedTest 和 @MethodSource 或 @ValueSource 对多个类运行相同的 JUnit 测试

java - 如何停止 Maven 的验证阶段重建工件?