spirng boot打包成war部署
2024-10-20 00:24:05
最近一段时间一直在研究和学习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,但是该类是一个抽象类,抽象类无法被实例化因此我们需要创建一个类并继承该类。
package com.torlight; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import javax.servlet.ServletContext;
import javax.servlet.ServletException; /**
* Created by acer on 2017-05-24.
* {@code TorlightSpringBootServletInitializer}继承于SpringBootServletInitializer 被 SpringServletContainerInitializer
* 使用
* @author acer
* @since 2017.05.24
*/
@Order(Ordered.HIGHEST_PRECEDENCE+)
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class TorlightSpringBootServletInitializer extends SpringBootServletInitializer{ private static final Logger logger= LoggerFactory.getLogger(TorlightSpringBootServletInitializer.class); @Override
public void onStartup(ServletContext servletContext) throws ServletException {
if(logger.isInfoEnabled()){
logger.info("TorlightSpringBootServletInitializer is startup");
}
super.onStartup(servletContext);
}
}
将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容器中
最新文章
- easyui Datagrid查询报错Uncaught TypeError:Cannot read property &#39;length&#39; of undefined
- Arduino 极速入门系列 - 光控灯(3) - 光敏电阻、与电阻分压那些事
- 理解jquery的$.extend()、$.fn和$.fn.extend()
- CSS 单行溢出文本显示省略号...的方法(兼容IE FF)(转)
- C++动态内存管理之shared_ptr、unique_ptr
- 什么情况下include_path不起作用?
- 暑假集训(5)第一弹——— Super Jumping! Jumping! Jumping!(hdu1087)
- SQL Server 扩展事件(Extented Events)从入门到进阶(1)——从SQL Trace到Extented Events
- artDialog.js的使用
- HDU 5875 Function(ST表+二分)
- [Android]SDK安装
- 高级软件工程2017第5次作业—— 团队项目:需求改进&;系统设计
- 安卓高级8 SurfaceView案例二 自定义相机
- java接口变量问题
- Ubuntu下录制和制作Gif图片--实战版
- Java消息中间件入门笔记 - ActiveMQ篇
- Spring注解方式配置Redis
- ssh访问服务器端visdom
- 【译】ASP.NET Identity Core 从零开始
- at com.mysql.jdbc.SQLError.createSQLException