2016-12-13 1352 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