面試分享:今天遇到一個面試官必定讓我講出spring boot經過war包的形式部署,一問就答不上來了,通常在項目上都是經過jar包的形式部署,如今總結一下:
步驟一:下載tomcat https://tomcat.apache.org/download-80.cgi
步驟二:重寫SpringBootServletInitializer啓動類的configure
默認狀況外部Tomcat不能讀取到Spring Boot 入口類的main方法啓動程序加載,使用須要繼承,代碼以下:java
@SpringBootApplication public class DefinedExternalTomcatApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DefinedExternalTomcatApplication.class, args); } /** * 重寫configure方法,加載啓動類 * @param builder * @return */ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DefinedExternalTomcatApplication.class); }
}mysql
步驟三:修改pom.xml文件
1.排除一下內置tomcat的jar包,防止打包以後與外面Tomcat jar 包衝突,致使項目啓動失敗,<exclusions></exclusion>
2.將<packaging>war</packaging>web
內容以下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.cqs</groupId> <artifactId>springbootDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 這裏打成war包 若打jar,需將war改成jar --> <packaging>war</packaging> <name>springbootDemo</name> <description>springboo外置tomcat</description> <!-- springboot依賴--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!-- springboot支持web--> <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的依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependencies> <!-- springboot操做數據庫--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- springboot與mysql驅動整合--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- springboot測試-> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>springbootDemo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
注意:將項目打成war包,部署到外部的tomcat中,這個時候,不能直接訪問spring boot 項目中配置文件配置的端口。application.yml中配置的server.port配置的是spring boot內置的tomcat的端口號, 打成war包部署在獨立的tomcat上以後, 你配置的server.port是不起做用的。必定要注意這一點!!面試
總結:war(java application archive)包與Jar(java application archive)包區別:spring
war包是基本web應用的應用程序(須要經過web應用在運行,如tomcat,Websphere、Jetty等) jar包是基於jar的應用程序(只須要安裝jdk就能夠)