介绍如何将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
源代码访问: https://github.com/tomlxq/gs-spring-boot/tree/master/spring-jar-war
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com