spring boot因為內嵌tomcat容器,所以可以通過打包為jar包的方法將項目發布,但是如何將spring boot項目打包成可發布到tomcat中的war包項目呢?
1. 既然需要打包成war包項目,首先需要在pom.xml文件中修改打包類型,將spring boot默認的<packaging>jar</packaging>修改為<packaging>war< /packaging>形式;
2. 其次spring boot的web項目中內嵌tomcat服務器,所以如果我們想要發布war包到tomcat項目,要講spring boot中內嵌的tomcat包依賴排除,不然產生沖突,打開下面代碼中的注釋即可。
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-web</
artifactId
>
<!--
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
-->
</
dependency
>
有一點想說的是,如果本地開發的時候依然想要使用spring boot內嵌tomcat進行調試,添加如下依賴即可;
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-tomcat</
artifactId
>
<
scope
>provided</
scope
>
</
dependency
>
SpringBootServletInitializer
子類,並覆蓋它的
configure
方法,或者直接將main函數所在的類繼承
SpringBootServletInitializer
子類,並覆蓋它的
configure
方法。代碼舉例如下,
@SpringBootApplication
public
class
DemoApplication
extends
SpringBootServletInitializer {
@Override
protected
SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return
application.sources(DemoApplication.
class
);
}
public
static
void
main(String[] args) {
SpringApplication.run(DemoApplication.
class
, args);
}
}
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
這里一定要是3.0以上才可以
7.在idea中點擊Run->Edit configuration->點擊+新添加一個Local Tomcat server

8.在Application Servers中的新添加的tomcat里添加Artifacts即可。
spring boot因為內嵌tomcat容器,所以可以通過打包為jar包的方法將項目發布,但是如何將spring boot項目打包成可發布到tomcat中的war包項目呢?
1. 既然需要打包成war包項目,首先需要在pom.xml文件中修改打包類型,將spring boot默認的<packaging>jar</packaging>修改為<packaging>war< /packaging>形式;
2. 其次spring boot的web項目中內嵌tomcat服務器,所以如果我們想要發布war包到tomcat項目,要講spring boot中內嵌的tomcat包依賴排除,不然產生沖突,打開下面代碼中的注釋即可。
1
2
3
4
5
6
7
8
9
10
11
12
|
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-web</
artifactId
>
<!--
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
-->
</
dependency
>
|
有一點想說的是,如果本地開發的時候依然想要使用spring boot內嵌tomcat進行調試,添加如下依賴即可;
1
2
3
4
5
|
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-tomcat</
artifactId
>
<
scope
>provided</
scope
>
</
dependency
>
|
3. spring boot發布jar包web程序的入口是main函數所在的類,使用@SpringBootApplication注解。但是如果war包發布至tomcat,需要增加 SpringBootServletInitializer
子類,並覆蓋它的 configure
方法,或者直接將main函數所在的類繼承 SpringBootServletInitializer
子類,並覆蓋它的 configure
方法。代碼舉例如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@SpringBootApplication
public
class
DemoApplication
extends
SpringBootServletInitializer {
@Override
protected
SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return
application.sources(DemoApplication.
class
);
}
public
static
void
main(String[] args) {
SpringApplication.run(DemoApplication.
class
, args);
}
}
|