Spring框架升级到5
Posted
技术标签:
【中文标题】Spring框架升级到5【英文标题】:Spring framework upgrade to 5 【发布时间】:2018-05-09 10:26:46 【问题描述】:我正在尝试将 Spring 4.0.1.RELEASE 升级到最新的 Spring 版本 5.0.6.RELEASE。在我的 pom.xml 中更改版本后,它显示错误
org.springframework.beans.factory.BeanDefinitionStoreException:从 ServletContext 资源 [/WEB-INF/spring/appServlet/servlet-context.xml] 解析 XML 文档时出现意外异常;嵌套异常是 java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.getEnvironment()Lorg/springframework/core/env/Environment;
它与 4.0.1.RELEASE 完美配合。
我的 pom.xml:
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.0.6.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>$org.springframework-version</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>$org.springframework-version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>$org.springframework-version</version>
</dependency>
<!-- Joda TIme dependency for date and time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<!-- Spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
和 servletContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:c='http://www.springframework.org/schema/c' xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:ldap="http://www.springframework.org/schema/ldap" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<context:component-scan base-package="com.abc.xyz" />
我错过了什么?
【问题讨论】:
【参考方案1】:你还没有更新你的 spring 安全包的版本
<version>3.1.3.RELEASE</version>
这就是问题发生的原因。你需要使用
<version>5.0.5.RELEASE</version>
查看针对相同错误发布的类似问题
Parsing applicationContext.xml renders a `java.lang.NoSuchMethodError`. Could this be caused by dependency issues?
【讨论】:
【参考方案2】:Spring Security 3.1.3.RELEASE 不支持 Spring 5.0.0 或更高版本。 Spring Security 5.0.0 支持 Spring Framework 5.0.0 或更高版本。
结帐Spring Security 5.0有什么新功能?在这里
https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#new
Spring 的日志记录设置也已针对 Spring 5 进行了修改。因此您不需要排除 commons-logging。 在此处查看日志记录部分
https://docs.spring.io/spring/docs/5.0.0.RC3/spring-framework-reference/overview.html
进行以下更改,您的应用就会像魅力一样工作。
注意:您也可以使用 Spring Security 5.0.5.RELEASE
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!-- Spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
【讨论】:
【参考方案3】:将 spring security 的版本从 3.1.3.RELEASE 更改为 5.0.5.RELEASE。在属性文件中添加 spring 安全版本,以便您以后不会错过更改它。
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.0.6.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
<org.springframework.security-version>5.0.5.RELEASE</org.springframework.security-version>
</properties>
【讨论】:
【参考方案4】:我建议你使用 Spring 5.0.0.RELEASE,这个版本对所有版本都有支持,你可以将这个依赖添加到你的 pom.xml 文件中,看起来像这样..
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- Spring WEB MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- Spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
【讨论】:
以上是关于Spring框架升级到5的主要内容,如果未能解决你的问题,请参考以下文章
老王读Spring IoC-5Spring IoC 小结——控制反转依赖注入
Spring 从入门到精通系列框架教程(这个掌握了考试就不怕了)❤️