tomLuo

加油!努力!奋斗!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

         介绍如何将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配置

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
posted on 2016-05-20 14:18  tomLuo  阅读(6094)  评论(0编辑  收藏  举报