spring boot 项目发包问题总结 (一)

170 阅读1分钟

问题:使用spring boot 打war包发布服务。报异常为 .......LifeCycleException。 问题解决办法为:去除内置的Tomcat

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!--去除内置的Tomcat-->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    

如果本地开发的时候依然想要使用spring boot内嵌tomcat进行调试,添加如下依赖即可

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

spring boot发布jar包web程序的入口是main函数所在的类,使用@SpringBootApplication注解。但是如果war包发布至tomcat,需要增加 SpringBootServletInitializer 子类,并覆盖它的 configure 方法,或者直接将main函数所在的类继承 SpringBootServletInitializer 子类,并覆盖它的 configure 方法。

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

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


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