无法访问org.springframework.web.WebApplicationInitializer

时间:2016-12-13 19:59:12

标签: java spring hibernate spring-mvc spring-boot

我目前正在使用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在指南中甚至没有提到时会导致此错误?

感谢。

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

您正在扩展实施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

在指南显示

时使用此Application类
@SpringBootApplication
@EnableScheduling
public class Application {

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