使用spring boot 2.0.1在tomcat中部署war时出现IllegalStateException

时间:2018-04-09 06:42:58

标签: spring-boot

我使用gradle + boot 2.0.1打包了一场战争,gradle脚本如下

plugins {
    id 'org.springframework.boot' version '2.0.1.RELEASE'
    id 'java'
    id 'war'
}

apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
    }
}

repositories {
    mavenCentral()
}
bootWar{
    mainClassName = 'api.abroad.Application'
}

configurations {
    providedRuntime
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

具有main功能的Application类如下

@SpringBootApplication
public class Application {

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

我还推出了一个类,它扩展了SpringBootServletInitializer以覆盖configure函数,尽管在docs中不需要它。

public class ServletInitializer extends SpringBootServletInitializer{

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

}

我在我的windows中安装了tomcat 8.5.29,并部署了战争。如下所示发生异常

2018-04-09 14:40:19.978  INFO 11720 --- [ost-startStop-1] com.tbiedu.abroad.ServletInitializer     : Started ServletInitializer in 4.113 seconds (JVM running for 10.228)
2018-04-09 14:40:19.989  INFO 11720 --- [ost-startStop-1] c.t.abroad.controller.DemoController     : Root context already created (using as parent).
09-Apr-2018 14:40:19.992 严重 [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/abroad-service-api]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: No SpringApplication sources have been defined. Either override the configure method or add an @Configuration annotation
        at org.springframework.util.Assert.state(Assert.java:73)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:127)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:87)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5204)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 10 more

我错过了什么......

1 个答案:

答案 0 :(得分:1)

您需要在此类上方放置@Configuration

@Configuration//--> this line u need to place 
public class ServletInitializer extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}
}