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 被 SpringServletContainerInitializer
18  * 使用
19  * @author acer
20  * @since 2017.05.24
21  */
22 @Order(Ordered.HIGHEST_PRECEDENCE+9999)
23 @SpringBootApplication
24 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
25 public class TorlightSpringBootServletInitializer extends SpringBootServletInitializer{
26 
27     private static final Logger logger= LoggerFactory.getLogger(TorlightSpringBootServletInitializer.class);
28 
29     @Override
30     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容器中
posted @ 2017-05-30 13:26  来自非洲大草原的食人虎  阅读(1698)  评论(0编辑  收藏  举报