Tomcat转SpringBoot、tomcat升级到springboot、springmvc改造springboot

news2025/2/5 22:13:49

Tomcat转SpringBoot、tomcat升级到springboot、springmvc改造springboot

起因:我接手tomcat-springmvc-hibernate项目,使用tomcat时问题不大。自从信创开始,部分市场使用国产中间件,例如第一次听说的宝兰德、东方通,还有一些市场使用weblogic、WebSphere;特别是商用中间件,难以满足本地运行并编写部署文档,只能靠市场自行摸搜适配。

那咋办?我直接把tomcat应用直接改造成springboot应用不就完事了,省去了中间件、中间商赚差价、虽然springboot底层用tomcat运行web,但没有了宝兰德、东方通的适配报错。

原项目:tomcat war+spring5+springmvc+hibernate+mysql
改造后:springboot+springbootweb+hibernate+mysql

添加依赖

第一步是添加springboot依赖

<!--	打包类型为jar	-->
<packaging>jar</packaging>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <!--	spring的版本应该与springboot内的spring版本一致	-->
  <spring.version>5.3.30</spring.version>
  <springboot.version>2.7.18</springboot.version>
  <hibernate.version>5.6.15.Final</hibernate.version>
</properties>

<dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>${springboot.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${springboot.version}</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    <version>${springboot.version}</version>
  </dependency>
  
  // 项目其他依赖
</dependencies>


<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>${springboot.version}</version>
  <configuration>
    <excludes>
      <exclude>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
      </exclude>
    </excludes>
    <mainClass>app.MyApplication</mainClass>
  </configuration>
</plugin>

原来是spring5.3.x,对应springboot2.7.x

转化web.xml

1、编写一个MyApplication

@SpringBootApplication(
        exclude = {
                RedisAutoConfiguration.class, SpringDataWebAutoConfiguration.class
        },
        scanBasePackages = {"app", "cn.com.xxxxx"}
)
@ImportResource({"classpath:spring.xml", "classpath:spring-ds.xml", "classpath:spring-validator.xml"})
@Slf4j
public class MyApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args);
        String port = context.getEnvironment().getProperty("server.port");
        if (port==null)
            port="8080";
        log.info("web: {}", "http://localhost:" +port);
    }
}
  • 特别注意,使用 @ImportResource 导入原有的xml配置
  • 还有包扫描路径 scanBasePackages
  • MyApplication 所放的位置有讲究,通常放到包的根目录下

2、创建一个 src/main/resources/application.properties

spring.main.allow-bean-definition-overriding=true
spring.main.allow-circular-references=true
spring.jpa.open-in-view=false
server.servlet.context-path=/app

允许bean循序、bean重写、web访问上下文路径

3、将web.xml中的 filter/Listener转化为bean

//使用RegistrationBean方式注入Listener
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        AppSessionListener myListener = new AppSessionListener();//创建原生的Listener对象
        return new ServletListenerRegistrationBean(myListener);
    }

    //使用RegistrationBean方式注入Listener
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean2() {
        AppServletContextListener myListener = new AppServletContextListener();//创建原生的Listener对象
        return new ServletListenerRegistrationBean(myListener);
    }

其他的就不多赘述

静态文件转发

tomcat应用的静态文件通常放在app/WebContent
例如app/WebContent/js/app.js,我们需要将它挂载到静态路径下:

/**
 * @author lingkang
 * created by 2023/12/8
 */
@Slf4j
@Configuration
public class StaticSourceConfig implements WebMvcConfigurer {
    /**
     * 部署本地资源到url
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        File file = new File(System.getProperty("user.dir") + File.separator + "WebContent");
        String path = file.getAbsolutePath();
        if (!path.endsWith("/"))
            path=path+File.separator;
        log.info("静态文件映射路径:{}", path);
        registry.addResourceHandler("/**")
                .addResourceLocations("file:" + path);
    }
}

打包

基于上面的,基本告一段落了,在没有移动前端资源的情况,同时我们的老项目有那么多xml配置,不可能将它打包到jar里,否则不符合修改迁移。这时候就要用到maven-assembly-plugin插件了,pom.xml配置如下:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.5.1</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <encoding>UTF-8</encoding>
    <compilerArgs>
      <arg>-parameters</arg>
      <arg>-extdirs</arg>
      <arg>${project.basedir}/WebContent/WEB-INF/lib</arg>
    </compilerArgs>
  </configuration>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.6.0</version>
  <configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <!-- 此处,要改成自己的程序入口(即 main 函数类) -->
      <manifest>
        <mainClass>app.MyApplication</mainClass>
      </manifest>
    </archive>
    <descriptors>
      <!--assembly配置文件路径,注意需要在项目中新建文件package.xml-->
      <descriptor>${project.basedir}/src/main/resources/package/package.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

src/main/resources/package/package.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <!--
        assembly 打包配置更多配置可参考官方文档:
            http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
     -->
    <id>release</id>

    <!--
        设置打包格式,可同时设置多种格式,常用格式有:dir、zip、tar、tar.gz
            dir 格式便于在本地测试打包结果
            zip 格式便于 windows 系统下解压运行
            tar、tar.gz 格式便于 linux 系统下解压运行
     -->
    <formats>
        <format>dir</format>
        <!--<format>zip</format>-->
        <!-- <format>tar.gz</format> -->
    </formats>

    <!-- 打 zip 设置为 true 时,会在 zip 包中生成一个根目录,打 dir 时设置为 false 少层目录 -->
    <!--<includeBaseDirectory>true</includeBaseDirectory>-->

    <fileSets>
        <!-- src/main/resources 全部 copy 到 config 目录下 -->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <!--包含那些依赖-->
            </includes>
        </fileSet>

        <!-- 项目根下面的脚本文件 copy 到根目录下 -->
        <fileSet>
            <directory>${basedir}/src/main/resources/package</directory>
            <outputDirectory></outputDirectory>
            <!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 -->
            <fileMode>755</fileMode>
            <lineEnding>unix</lineEnding>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${basedir}/WebContent/WEB-INF/lib</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <!--包含那些依赖-->
                <include>*.jar</include>
            </includes>
        </fileSet>
        <!-- 静态资源 awb.operations.config.StaticSourceConfig -->
        <fileSet>
            <directory>${basedir}/WebContent</directory>
            <outputDirectory>WebContent</outputDirectory>
            <includes>
                <!--包含那些依赖-->
                <include>compressor/**</include>
                <include>conf/**</include>
                <include>dependence/**</include>
                <include>elementui/**</include>
                <include>fonts/**</include>
                <include>icons/**</include>
                <include>image/**</include>
                <include>img/**</include>
                <include>module/**</include>
                <include>nodejs/**</include>
                <include>script/**</include>
                <include>*.js</include>
                <include>*.html</include>
                <include>*.css</include>
                <include>*.json</include>
            </includes>
        </fileSet>


    </fileSets>

    <!-- 依赖的 jar 包 copy 到 lib 目录下 -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>

</assembly>

/src/main/resources/package/start.sh运行内容如下

#!/bin/bash
# ----------------------------------------------------------------------
#
# 使用说明:
# 1: 该脚本使用前需要首先修改 MAIN_CLASS 值,使其指向实际的启动类
#
# 2:使用命令行 ./start.sh start | stop | restart 可启动/关闭/重启项目
#
#
# 3: JAVA_OPTS 可传入标准的 java 命令行参数,例如 -Xms256m -Xmx1024m 这类常用参数
#
# 4: 函数 start() 给出了 4 种启动项目的命令行,根据注释中的提示自行选择合适的方式
#
# ----------------------------------------------------------------------

# 启动入口类,该脚本文件用于别的项目时要改这里
MAIN_CLASS=app.MyApplication

if [[ "$MAIN_CLASS" == "app.MyApplication" ]]; then
    echo "请先修改 MAIN_CLASS 的值为你自己项目启动Class,然后再执行此脚本。"
	exit 0
fi

COMMAND="$1"

if [[ "$COMMAND" != "start" ]] && [[ "$COMMAND" != "stop" ]] && [[ "$COMMAND" != "restart" ]]; then
#	echo "Usage: $0 start | stop | restart , 例如: sh start.sh start / sh start.sh stop"
#	exit 0
COMMAND="start"
fi


# Java 命令行参数,根据需要开启下面的配置,改成自己需要的,注意等号前后不能有空格
JAVA_OPTS="-Xms512m -Xmx2048m "
# JAVA_OPTS="-Dserver.port=80 "

# 生成 class path 值
APP_BASE_PATH=$(cd `dirname $0`; pwd)
CP=${APP_BASE_PATH}/config:${APP_BASE_PATH}/lib/*

function start()
{
    # 运行为后台进程,并在控制台输出信息
    #java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} &

    # 运行为后台进程,并且不在控制台输出信息
    # nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} >/dev/null 2>&1 &

    # 运行为后台进程,并且将信息输出到 output.out 文件
    nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} > nohup.out &

    # 运行为非后台进程,多用于开发阶段,快捷键 ctrl + c 可停止服务
    # java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS}
}

function stop()
{
    # 支持集群部署
    kill `pgrep -f ${APP_BASE_PATH}` 2>/dev/null

    # kill 命令不使用 -9 参数时,会回调 onStop() 方法,确定不需要此回调建议使用 -9 参数
    # kill `pgrep -f ${MAIN_CLASS}` 2>/dev/null

    # 以下代码与上述代码等价
    # kill $(pgrep -f ${MAIN_CLASS}) 2>/dev/null
}

if [[ "$COMMAND" == "start" ]]; then
	start
elif [[ "$COMMAND" == "stop" ]]; then
    stop
else
    stop
    start
fi

这时候就能执行打包了:
在这里插入图片描述
或者

mvn package

效果如下
在这里插入图片描述

然后将整个路面打包成zip传到服务器运行即可

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1328095.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

众和策略:美股全线上涨 中概股大涨

当地时间12月21日&#xff0c;欧洲股市全线下跌&#xff0c;英国富时100指数、法国CAC40指数、德国DAX指数均小幅下跌。美国通胀降温&#xff0c;美股商场三大指数尾盘飙升&#xff0c;纳斯达克指数、标普500指数均涨逾1%&#xff0c;大型科技股多数上涨&#xff0c;特斯拉涨近…

Jenkins自动化构建打包,部署

1.环境准备 上传jdk&#xff0c;maven和tomcat的包&#xff0c;解压到/usr/local下并配置环境变量。 配置jdk [rootserver04 ~]# vim /etc/profile.d/java.sh JAVA_HOME/usr/local/java export PATH$JAVA_HOME/bin:$PATH##加载环境变量 [rootserver04 ~]# source /etc/profi…

Mybatis之增删改查

一、引言 书接上回&#xff0c;我们在了解完mybatis之后&#xff0c;肯定要知道怎么使用&#xff0c;本文就来详细讲解Mybatis的增删改查事务&#xff0c;还不了解怎么配置mybatis的童鞋可以去这篇文章了解一下通俗易懂讲解javaweb之mybatis-CSDN博客 二、Mybatis——增 举例…

Android笔记(二十):JetPack DataStore 之 Proto DataStore

Jetpack DataStore 是一种数据存储解决方案&#xff0c;主要适用于小型数据的处理。它可以通过协议缓冲区存储键值对或类型化对象。DataStore 使用 Kotlin 协程和 Flow 以异步、一致的事务方式存储数据。DataStore有两种实现方式&#xff08;1&#xff09;Preferences DataStor…

基于ssm房屋租赁平台的设计与开发论文

摘 要 目前对于在外的人员来说租赁房屋是最基本的问题。对于房屋的租赁可以选择直接找房东、找专业的房屋租赁公司和自己在网上找房屋。自己找房东的问题在于需要时间&#xff0c;而且对于需要提前租赁房屋的需要多次跑到小区&#xff0c;找中介租赁房屋的问题在于费用问题&am…

消除蛋蛋派

欢迎来到程序小院 消除蛋蛋派 玩法&#xff1a;消除游戏&#xff0c;三个相同形状的蛋蛋连成一条直线即可消除&#xff0c;点击鼠标左键移动球球进行消除&#xff0c; 可以使用道具&#xff0c;共有50关卡&#xff0c;快去闯关吧^^。开始游戏https://www.ormcc.com/play/gameS…

Python爬取电影天堂

前言&#xff1a; 本文非常浅显易懂&#xff0c;可以说是零基础也可快速掌握。如有疑问&#xff0c;欢迎留言&#xff0c;笔者会第一时间回复。 一、爬虫的重要性&#xff1a; 如果把互联网比喻成一个蜘蛛网&#xff0c;那么Spider就是在网上爬来爬去的蜘蛛。网络蜘蛛通过网页的…

FFmpeg windows安装与使用

FFmpeg下载&#xff1a; 1、进入ffmpeg官网&#xff0c;点击“Download”。官网地址&#xff1a;FFmpeg 2、选择对应环境的编译工具&#xff0c;如下载windows环境下的ffmpeg编译工具 3、点击下载编译好的ffmpeg工具 FFmpeg使用&#xff1a; 1、将ffmpeg编译的bin文件复制出来…

如何用Excel制作一张能在网上浏览的动态数据报表

前言 如今各类BI产品大行其道&#xff0c;“数据可视化”成为一个热门词汇。相比价格高昂的各种BI软件&#xff0c;用Excel来制作动态报表就更加经济便捷。今天小编就将为大家介绍一下如何使用葡萄城公司的纯前端表格控件——SpreadJS来实现一个Excel动态报表&#xff1a; 实…

uniapp 添加分包页面,配置分包预下载

为什么要分包 ? 分包即将小程序代码分成多个部分打包&#xff0c;可以减少小程序的加载时间&#xff0c;提升用户体验 添加分包页面 比较便捷的方法是使用vscode插件 uni-create-view 新建分包文件夹 以在我的页面&#xff0c;添加分包的设置页面为例&#xff0c;新建文件夹 s…

五、Microsoft群集服务(MSCS)环境的搭建

一、【目的】 学会利用Windows Server布置群集环境。 二、【设备】 FreeNAS11.2&#xff0c;Windows Server 2019 三、【要求】 学会利用Windows Server布置群集环境&#xff0c;掌握处理问题的能力。 配置表&#xff1a; 节点公网IP(public)内网IP(private)群集IP(clust…

RocketMQ系统性学习-RocketMQ高级特性之文件恢复与 CheckPoint 机制

&#x1f308;&#x1f308;&#x1f308;&#x1f308;&#x1f308;&#x1f308;&#x1f308;&#x1f308; 【11来了】文章导读地址&#xff1a;点击查看文章导读&#xff01; &#x1f341;&#x1f341;&#x1f341;&#x1f341;&#x1f341;&#x1f341;&#x1f3…

理解AI思维链:AI领域的核心概念及其意义

理解AI思维链&#xff1a;AI领域的核心概念及其意义 引言AI思维链的定义AI思维链的重要性实际应用案例分析面临的挑战与未来展望结语 引言 在这个日益由数据驱动的时代&#xff0c;人工智能&#xff08;AI&#xff09;已经成为科技领域的一颗耀眼的明星&#xff0c;其影响力遍…

TG5032CGN TCXO / VC-TCXO(超高稳定10pin端子型)

TG5032CGN 晶振是EPSON推出的一款额定频率10MHz至40MHz的石英晶体振荡器&#xff0c;该型号采用互补金属氧化物半导体技术&#xff0c;输出波形稳定可靠。外形尺寸为5.0 3.2 1.45mm具有小尺寸,高稳定性。该款晶体振荡器&#xff0c;可以在G&#xff1a;-40C至 85C的温度内稳定…

达梦到达梦的外部链接dblink(DM-DM DBLINK)

一. 使用场景&#xff1a; 部链接对象&#xff08;LINK&#xff09;是 DM 中的一种特殊的数据库实体对象&#xff0c;它记录了远程数据库的连接和路径信息&#xff0c;用于建立与远程数据的联系。通过多台数据库主库间的相互通讯&#xff0c;用户可以透明地操作远程数据库的数…

v-if与v-show的区别

v-if指令可以控制一个元素的显示和隐藏&#xff0c;那么它是如何实现的&#xff1f;它和看起来很像的v-show指令有什么区别呢&#xff1f; 如果v-if指令的值为假&#xff0c;那么这个元素不会被插入DOM。 下面的代码 <div v-if"true">one</div><div…

海外社媒营销新趋势,品牌出海如何做?

社交媒体在网上的影响力是毋庸置疑的。投资社交媒体平台并建立公司形象&#xff0c;提高产品运营收入&#xff0c;提升品牌知名度&#xff0c;对于吸引对您所提供的产品感兴趣的人至关重要。 然而&#xff0c;社交媒体格局总是在变化&#xff0c;这意味着您需要掌握新的社交媒…

【Jmeter】循环执行某个接口,接口引用的参数变量存在规律变化

变量设置成下面的值即可 ${__V(supplierId_${supplierIdNum})}

【项目问题解决】% sql注入问题

目录 【项目问题解决】% sql注入问题 1.问题描述2.问题原因3.解决思路4.解决方案1.前端限制传入特殊字符2.后端拦截特殊字符-正则表达式3.后端拦截特殊字符-拦截器 5.总结6.参考 文章所属专区 项目问题解决 1.问题描述 在处理接口入参的一些sql注入问题&#xff0c;虽然通过M…

基于SSM的在线学习系统的设计与实现论文

基于SSM的在线学习系统的设计与实现 摘要 随着信息互联网购物的飞速发展&#xff0c;一般企业都去创建属于自己的管理系统。本文介绍了在线学习系统的开发全过程。通过分析企业对于在线学习系统的需求&#xff0c;创建了一个计算机管理在线学习系统的方案。文章介绍了在线学习…