1. 程式人生 > >Spring Boot以War包啟動

Spring Boot以War包啟動

tom XML 現在 prot 使用 驗證過 servle cati ack

1.IDEA Spring Initializer自動構建的war包項目,自動生成的Initializer類,用於外部Tomcat容器啟動該項目時調用,如果仍然使用主類main函數方式啟動則與此類無關(Debug驗證過了)

2.自動構建的war包項目,pom.xml中引入了:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>

註釋的scope是我註釋的,生成時打開著,這樣顯式引入的仍然是Spring Boot內嵌Tomcat,scope造成運行時沒有內嵌Tomcat(只有編譯時有),雖然引入了:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

仍然提示錯誤:

Unregistering JMX-exposed beans on shutdown

所以需要註釋掉scope,這樣內嵌的Tomcat可以啟動Spring Boot Web項目,但打war包放在外置Tomcat時就不需要了,要使用exclude幹掉內嵌Tomcat,或像現在這樣使用scope在運行時不使用內嵌Tomcat。這時需要外置Tomcat使用這裏生成的Initializer類:

package com.xiaobai.springbootwebdemo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootwebdemoApplication.class);
}

}

Initializer類分析:

Spring Boot以War包啟動