Thursday, May 24, 2018

Spring framework upgrade to 5

Leave a Comment

I am trying to upgrade Spring 4.0.1.RELEASE to latest spring version 5.0.6.RELEASE. After changing version in my pom.xml it is showing error

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.getEnvironment()Lorg/springframework/core/env/Environment;

It's working perfectly fine with 4.0.1.RELEASE.

My 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> 

and 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" /> 

What am I missing?

4 Answers

Answers 1

You have not updated the version of your spring security packages

<version>3.1.3.RELEASE</version> 

That is why the issue occurs. You need to use

<version>5.0.5.RELEASE</version> 

Spring versions

See a similar question posted on the same error

Parsing applicationContext.xml renders a `java.lang.NoSuchMethodError`. Could this be caused by dependency issues?

Answers 2

Spring Security 3.1.3.RELEASE does not support Spring 5.0.0 or greater. Spring Security 5.0.0 has support for Spring Framework 5.0.0 or greater.

Checkout What’s New in Spring Security 5.0 ? here

https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#new

Also Spring’s logging setup has been revised for Spring 5. So you don't need to exclude commons-logging. Checkout the logging section here

https://docs.spring.io/spring/docs/5.0.0.RC3/spring-framework-reference/overview.html

Do the below changes and your app with work like a charm.

Note: You could also use 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> 

Answers 3

Change the version of spring security from 3.1.3.RELEASE to 5.0.5.RELEASE. Add spring security version in properties file so that you don't miss to change it in future.

<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> 

Answers 4

I would suggest you to use Spring 5.0.0.RELEASE, This version has support for all and you may add this dependency to your pom.xml file will be look like this ..

<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> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment