首页 技术 正文
技术 2022年11月9日
0 收藏 508 点赞 3,644 浏览 2099 个字

  最近一段时间一直在研究和学习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容器中
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,906
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,739
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,130
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293