springboot war包在tomcat中運行

將springboot微服務部署在tomcat-8.5.23版本上。如下爲修改配置信息。java

一、引包pom.xml

<dependencies>
    <!--修改包引用不包含tomcat-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!--網上有說須要排除spring-boot-starter-tomcat,但我在tomcat-8.5.23下不排除也能夠-->
        <!--<exclusions>-->
        <!--    <exclusion>-->
        <!--        <groupId>org.springframework.boot</groupId>-->
        <!--        <artifactId>spring-boot-starter-tomcat</artifactId>-->
        <!--    </exclusion>-->
        <!--</exclusions>-->
    </dependency>
    <!--war包部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <!--<version>1.5.1.RELEASE</version>-->
        <!--<scope>provided</scope>-->
    </dependency>
</dependencies>
    
<!--修改打包設置-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding>
                <skipTests>true</skipTests>
                <!-- 打成war包時名子 -->
                <warName>ROOT</warName>
                <!-- mave時啓用的main路徑(由於其餘包裏面測試時也加了main函數) -->
                <mainClass>${start-class}</mainClass>
                <skip>true</skip><!-- 跳過測試 -->
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>
    </plugins>
</build>

二、修改啓動類

package com.pengei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;

# 第一步:extends SpringBootServletInitializer
@SpringBootApplication
@EnableCaching
public class MyAPIApplication extends SpringBootServletInitializer {
    # 第二步override configure方法
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyAPIApplication.class);
    }

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

三、增長/webapp/WEB-INF/web.xml,內容以下

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.pengei.MyAPIApplication</param-value>
</context-param>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextAttribute</param-name>
        <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
相關文章
相關標籤/搜索