介紹如何將spring boot jar轉成war
spring boot 默認是以jar包形式啟動web程序,在新建spring boot項目時候可以選擇war包的啟動方式。 建議在開發的時候建立以jar包啟動的web項目,啟動效率更快,此時如果想發布成war包形式部署,需要做以下幾步:
1 在pom.xml中將packaging的值修改為war
<packaging>jar</packaging>
改成
<packaging>war</packaging>
2 排除嵌入的tomcat包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
改成
<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>
在這里需要移除對嵌入式Tomcat的依賴,這樣打出的WAR包中,在lib目錄下才不會包含Tomcat相關的JAR包,否則將會出現啟動錯誤。另外,在移除對Tomcat的依賴后,為了保證編譯正確,還需要添加對servlet-api的依賴
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
在這里將scope屬性設置為provided,這樣在最終形成的WAR中不會包含這個JAR包,因為Tomcat或Jetty等服務器在運行時將會提供相關的API類。此時,執行mvn package命令就會得到一個WAR文件,我們可以直接將其放到Tomcat下運行。
4 新增ServletInitializer類
src/main/java/com/example/config/ServletInitializer.java
package com.example.config;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
/** * User: TOM * Date: 2016/5/20 * email: beauty9235@gmail.com * Time: 8:51*/
@Configuration
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringJarWarApplication.class);
}
}
下面是我們測試這個war
創建一個測試頁
src/main/resources/templates/index.ftl
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Study spring boot from jar to war</title>
</head>
<body>
<p>Hell tomLuo!</p>
<p>This is a web application, we will introduce how to convert spring boot jar for spring boot war</p>
</body>
</html>
創建快捷的頁面轉向定義
將首頁定位到src/main/resources/templates/index.ftl
/src/main/java/com/example/config/WebMvcConfig.java
package com.example.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** * User: TOM * Date: 2016/5/20 * email: beauty9235@gmail.com * Time: 9:19 */
@Configuration
@ComponentScan(basePackages = { "com.example.web" }, useDefaultFilters = false, includeFilters = @Filter({ Controller.class }))
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter
{
Logger logger= LoggerFactory.getLogger(WebMvcConfig.class);
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index");
//添加更多
}
}
Tomcat7配置
pom.xml加上插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
tomcat配置
tomcat熱布署
-javaagent:[your_path]\jrebel.jar -Xmx512M -Xms512M -XX:MaxPermSize=1024m -noverify
系統啟動后我們會看到 # 打印出結果
Hell tomLuo! This is a web application, we will introduce how to convert spring boot jar for spring boot war