Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring framework upgrade to 5

Tags:

java

spring

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?

like image 586
Prashant Thorat Avatar asked May 09 '18 10:05

Prashant Thorat


People also ask

Is Spring 5 compatible with older versions of Java?

Spring 5 supports Java EE 7 and also compatible with Java EE 8. So we can use Servlet 4.0, Bean Validation 2.0, JPA 2.2 in our applications. We can also use their older versions i.e. Servlet 3.1, Bean Validation 1.1, JPA 2.1. Spring 5 applications preferred server versions are Tomcat 8.5+, Jetty 9.4+ and WildFly 10+.

What is difference between Spring 4 and Spring 5?

So Spring 4 had to support Java 6, 7 and 8. To maintain the backward compatibility, Spring framework didn't adapted many new features which Java 8 brought with itself e.g. Lambda programming. Spring 5 has baseline version 8, so it uses many new features of Java 8 and 9 as well.

Does Spring 4 supports hibernate 5?

Note that Hibernate 4.3 is a JPA 2.1 provider and therefore only supported as of Spring Framework 4.0. The same applies to Hibernate Validator 5.0 as a Bean Validation 1.1 provider. Neither of the two are officially supported with Spring Framework 3.2.


2 Answers

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?

like image 100
Tarun Lalwani Avatar answered Sep 21 '22 03:09

Tarun Lalwani


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>
like image 44
Shubham Kadlag Avatar answered Sep 22 '22 03:09

Shubham Kadlag