idea中使用tomcat 方式启动spring boot项目

时间:2024-03-09 07:18:39

 

 Spring boot 的main 入口启动方式相信都会用,直接运行main直接就启动了,但是往往这种方式并不是最佳的启动方式,比如运维的层面更希望调整tomcat的调优参数,而只使用嵌入启动方式很难做到这些。所以使用tomcat方式启动spring boot就比较重要。

 

1、去tomcat 官网下载 tomcat 8 tar.gz 文件,然后解压.

 https://tomcat.apache.org/download-80.cgi

2、idea中,配置启动

 

 

3、gradle or maven 配置

<packaging>war</packaging>

or

apply plugin: "war"

 

4、增加tomcat启动调用spring boot初始化入口:

public class ServletInitializer extends SpringBootServletInitializer {

    private Logger logger = LoggerFactory.getLogger(ServletInitializer.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        logger.info("starting spring boot initializer ......");

        return application.sources(MainApplication.class);
    }

}

 or

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

 

5、start runing

 搞定!