首页 > 代码库 > spirng boot打包成war部署
spirng boot打包成war部署
最近一段时间一直在研究和学习springboot,感觉其十分便利好用。以前使用spring搭建项目都整好多繁琐的配置,使用了springboot后这些繁琐的配置统统都不要了。但就是对springboot部署的方式感觉有点不爽,还是比较喜欢打包成war来进行部署。
在spring中有这样一个类:org.springframework.web.SpringServletContainerInitializer,阅读该类的注释发现该类的作用是使用编程方式的替代web.xml的。其原理就在于该类位于spring-web-xxx.jar中的META-INF/services有一个遵照java spi规范的文件,在支持servlet 3以上版本的j2ee容器中会读取该SPI服务的实现类SpringServletContainerInitializer。springboot已经为我们扩展了该类SpringBootServletInitializer,但是该类是一个抽象类,抽象类无法被实例化因此我们需要创建一个类并继承该类。
1 package com.torlight; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 import org.springframework.boot.autoconfigure.SpringBootApplication; 7 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 8 import org.springframework.boot.web.support.SpringBootServletInitializer; 9 import org.springframework.core.Ordered;10 import org.springframework.core.annotation.Order;11 12 import javax.servlet.ServletContext;13 import javax.servlet.ServletException;14 15 /**16 * Created by acer on 2017-05-24.17 * {@code TorlightSpringBootServletInitializer}继承于SpringBootServletInitializer 被 SpringServletContainerInitializer18 * 使用19 * @author acer20 * @since 2017.05.2421 */22 @Order(Ordered.HIGHEST_PRECEDENCE+9999)23 @SpringBootApplication24 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})25 public class TorlightSpringBootServletInitializer extends SpringBootServletInitializer{26 27 private static final Logger logger= LoggerFactory.getLogger(TorlightSpringBootServletInitializer.class);28 29 @Override30 public void onStartup(ServletContext servletContext) throws ServletException {31 if(logger.isInfoEnabled()){32 logger.info("TorlightSpringBootServletInitializer is startup");33 }34 super.onStartup(servletContext);35 }36 }
将pom.xml文件中packaging由jar改成war
由于打包成war独立部署,将spring-boot-starter-tomcat 的范围改成provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
移除spring-boot-maven-plugin maven插件
<!--
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
将静态文件按照war中的规范存放到webapp目录
完成上面的步骤后,执行maven打包命令将输出的war部署j2ee容器中
spirng boot打包成war部署
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。