BladeX-Boot项目已war包部署,还会启动内嵌的Tomcat?该如何配置

Blade 未结 3 3205
ghostfevil
ghostfevil 2019-04-25 13:31
悬赏:5

目前项目已经做完,打算以War包部署,按照SpringBoot常规配置完后发现会默认启动内嵌Tomcat。

常规配置如下:

----------------------------------------------------------------------------------------------------------

一、修改pom.xml文件将默认的jar方式改为war:


com.example

application

0.0.1-SNAPSHOT

war


二、排除内置的Tomcat容器(两种方式都可):


1.排除spring-boot-starter-web中的Tomcat


    org.springframework.boot

    spring-boot-starter-web

   

       

            org.springframework.boot

            spring-boot-starter-tomcat

       

   


2.添加依赖


    org.springframework.boot

    spring-boot-starter-tomcat

   

    provided


三、继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer,实现configure方法:


为什么继承该类,SpringBootServletInitializer源码注释:


Note that a WebApplicationInitializer is only needed if you are building a war file and deploying it. 


If you prefer to run an embedded web server then you won't need this at all.


注意,如果您正在构建WAR文件并部署它,则需要WebApplicationInitializer。


如果你喜欢运行一个嵌入式Web服务器,那么你根本不需要这个。


启动类代码:


@SpringBootApplication

public class Application {


    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }


}


1.方式一,启动类继承SpringBootServletInitializer实现configure:


@SpringBootApplication

public class Application extends SpringBootServletInitializer {


    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }


    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

        return builder.sources(Application.class);

    }

}


2.方式二,新增加一个类继承SpringBootServletInitializer实现configure:


public class ServletInitializer extends SpringBootServletInitializer {


    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

        //此处的Application.class为带有@SpringBootApplication注解的启动类

        return builder.sources(Application.class);

    }


}

----------------------------------------------------------------------------------------------------------

该如何配置才可以不启动内嵌Tomcat?

3条回答
提交回复