2016-12-13 1353 views
1

我目前正在通过使用Spring 4,Java 1.8和Gradle(https://spring.io/guides/gs/scheduling-tasks/)计划任务的示例。无法访问org.springframework.web.WebApplicationInitializer

然而,运行这个例子的时候,我收到以下错误:

Error:(11, 8) java: cannot access org.springframework.web.WebApplicationInitializer class file for org.springframework.web.WebApplicationInitializer not found

的源代码,以我的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在甚至连男人都不会造成这个错误在指南中?

谢谢。

+0

你可以发布你的gradle文件吗 –

+0

@KlausGroenbaek当然,我已经添加到我原来的帖子了。谢谢。 – 000000000000000000000

回答

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

良好的联系。现在这是有道理的。谢谢。 – 000000000000000000000

0

该指南不延长SpringBootServletInitializer,删除或添加spring-boot-starter-web作为一个依赖项,如果你想在你的应用程序中启动一个Tomcat web服务器离子。

+0

谢谢。我添加了依赖关系,现在它按照我想要的方式工作。 – 000000000000000000000