从Spring 3.2迁移至Spring 4.3分步指导请求(无Maven、无Hibernate)
我帮你整理了非Maven环境下从Spring 3.2迁移到4.3的完整步骤,不用Hibernate的话可以跳过ORM相关内容,放心跟着走就行:
一、准备Spring 4.3核心JAR包
首先要完全移除项目中所有Spring 3.2版本的JAR文件,彻底避免版本冲突。然后添加以下Spring 4.3.x(建议选最新的稳定版,比如4.3.30.RELEASE,兼容性和稳定性更好)的JAR:
核心容器模块(必选)
spring-core-4.3.x.RELEASE.jarspring-beans-4.3.x.RELEASE.jarspring-context-4.3.x.RELEASE.jarspring-expression-4.3.x.RELEASE.jar
Web模块(必选,Web应用必备)
spring-web-4.3.x.RELEASE.jarspring-webmvc-4.3.x.RELEASE.jar
可选模块(根据实际使用场景添加)
- 用到AOP时:
spring-aop-4.3.x.RELEASE.jar、spring-aspects-4.3.x.RELEASE.jar(搭配AspectJ使用) - 用到JDBC/事务时:
spring-jdbc-4.3.x.RELEASE.jar、spring-tx-4.3.x.RELEASE.jar - 用到缓存时:
spring-context-support-4.3.x.RELEASE.jar - 日志适配:
jcl-over-slf4j.jar(替换旧的commons-logging,避免日志冲突,搭配SLF4J实现如logback-classic.jar使用)
注意:Spring 4.3已经内置了CGLIB和ASM的相关类,不需要再单独引入
cglib.jar或asm.jar了。
二、修改web.xml配置
确认核心类引用(类名不变,确保JAR是4.3版本)
无需修改类名,但要保证使用的是4.3版本JAR中的类,示例配置如下:<!-- Spring上下文监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC前端控制器 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>升级Servlet Schema版本(可选但推荐)
如果你的web.xml还是使用Servlet 2.5的schema,建议升级到Servlet 3.0+,适配Spring 4.3的特性:<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">检查上下文配置路径
确保contextConfigLocation指向的配置文件路径正确,后续会更新这些配置文件的schema版本。
三、修改appContext.xml及其他Spring配置文件
更新所有Spring Schema命名空间版本
把配置文件开头的所有Spring schema版本从3.2替换为4.3,示例如下:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">移除废弃配置项
检查配置中是否有Spring 3.2标记为废弃的属性或标签:- 避免使用已废弃的
SimpleJdbcTemplate,替换为JdbcTemplate(API基本兼容) - 如果用了
<context:component-scan>的旧写法,保持现有逻辑即可,4.3完全兼容
- 避免使用已废弃的
事务配置检查
XML方式的事务配置写法没有变化,确保schema版本正确即可,示例:<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice>
四、其他注意事项
检查自定义代码的API依赖
查看项目中自定义类是否使用了Spring 3.2中废弃或移除的API:- 替换
DefaultAnnotationHandlerMapping为RequestMappingHandlerMapping - 替换
AnnotationMethodHandlerAdapter为RequestMappingHandlerAdapter
- 替换
测试验证
部署项目后优先测试核心功能:请求转发、数据库操作(若用JDBC)、事务控制等。如果遇到ClassNotFoundException或NoSuchMethodError,大概率是JAR包没替换完全或漏加了必要依赖。
内容的提问来源于stack exchange,提问作者Anurag

