何亦冷

导航

SpringBoot工程可执行jar修改为可执行war

三步骤简述

1. 主程序继承SpringBootServletInitializer,并重写configure方法

2. POM文件,打包为jar改为打包为war。<packaging>war</packaging>

3. 内嵌的Servlet容器scope改为provided。<scope>provided</scope>

 

结果:war包即可以放在Servlet容器,比如tomcat的webapps下面运行,也可以通过jar -jar demo.war的方式运行

参考:官方文档 https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-create-a-deployable-war-file

@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);
    }
}

--------------------------------------------------------------------------
    <groupId>com.hepengju</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1</version>

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

 

SpringBoot的一些有意思的知识

1. dev-tools的触发文件名称为.reloadtrigger时, STS,IDEA的终端会出现reload图标
2. 可执行war包的处理:主程序类继承SpringBootServlet初始化器,pom改为war,tomcat改为provided
3. StopWatch打印每个任务的耗时及百分比
4. 获取程序PID:ApplicationPid、StartupInfoLogger.getStartingMessage()、ApplicationPidFileWriter
5. lsof -i:6379 查询占用这个端口的文件

posted on 2020-01-31 22:19  何亦冷  阅读(528)  评论(0编辑  收藏  举报