无法访问org.springframework.web.WebApplicationInitializer

问题描述 投票:0回答:4

我目前正在研究使用 Spring 4、Java 1.8 和 Gradle 调度任务的示例 (https://spring.io/guides/gs/scheduling-tasks/)。

但是,在运行此示例时,我收到以下错误:

错误:(11, 8) java: 无法访问 org.springframework.web.WebApplicationInitializer 找不到 org.springframework.web.WebApplicationInitializer 的类文件

我的Application.java的源代码如下:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

我的gradle文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

jar {
    baseName = 'gs-scheduling-tasks'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

为什么 org.springframework.web.WebApplicationInitializer 会在指南中没有提及的情况下导致此错误?

谢谢。

java spring hibernate spring-mvc spring-boot
4个回答
10
投票

添加此依赖项以解决此问题。我完美地为我工作。

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

8
投票

本指南不会扩展 SpringBootServletInitializer,如果您想在应用程序中启动 Tomcat Web 服务器,请将其删除或添加 spring-boot-starter-web 作为依赖项。


1
投票

您正在扩展

SpringBootServletInitializer
,它实现了
WebApplicationInitializer

检查http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.boot/spring-boot/1.0.2.RELEASE/org/springframework/boot/context/web/SpringBootServletInitializer.java

按照指南所示使用此应用程序类

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

0
投票

正如@Sully 指出的,

SpringBootServletInitializer
确实实现了
WebApplicationInitializer
,但两者都在不同的 JAR-s(工件)中。当您想要将 WAR 文件部署到独立的 Tomcat 时,不使用
spring-boot-starter-web
是完全合理的——否则您将包含 Tomcat 的引擎(除非您明确地将其从依赖项中排除)。 如果是这样的话,那么我建议放入父POM:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>${servlet-api.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

然后在你的“网络模块”项目中:

  <dependencies>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
    </dependency>
  </dependencies>

spring-boot-dependencies
提供了 spring-boot-starter 和 spring-web (包含缺少的类)。但为了使一切正常工作,您还需要提供 servlet API,因此我们也添加了该 API。

通过这种方法,您可以获得最小的 WAR 大小,并且不必记住排除 Tomcat 依赖项。

© www.soinside.com 2019 - 2024. All rights reserved.