1. 程式人生 > 實用技巧 >Spring Boot之使用外部tomcat

Spring Boot之使用外部tomcat

一般在生產環境都會部署在外部的web容器,比如我們公司就部署在WildFly。想這麼做就要改一些配置

一 啟動類的修改

public class Application extends SpringBootServletInitializer {
    private static final Logger appLogger = LoggerFactory.getLogger(Application.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        
return application.sources(Application.class); } public static void main(String[] args) { if (appLogger.isInfoEnabled()) { appLogger.info("snrs-web start."); } SpringApplication sa = new SpringApplication(Application.class); sa.setWebEnvironment(
true); sa.run(args); } }

  比如繼承SpringBootServletInitializer 同時實現方法configure

二 pom檔案的修改

  對於SpringBoot1版本要排除內建的tomcat

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat.embed</groupId>
                    <artifactId>tomcat-embed-websocket</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

SpringBoot2的寫法是

<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>
<!--新增servlet-api依賴--->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>//表示使用JBoss或者Tomcat自帶的jar
</dependency>

  同時要增加對各個環境打包的配置

  

<profiles>
        <profile>
            <id>dev</id>

            <properties>
                <package>jar</package>
            </properties>

            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <build>
                <filters>
                    <filter>src/main/filters/filter-dev.properties</filter>
                </filters>
            </build>
        </profile>
                
                <profile>
            <id>prd</id>

            <properties>
                <package>war</package>
            </properties>

            <build>
                <filters>
                    <filter>src/main/filters/filter-prod.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <excludes>
                            <exclude>rsf.consumer.xml</exclude>
                            <exclude>rsf.provider.xml</exclude>
                            <exclude>application.properties</exclude>
                        </excludes>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>