Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading To Spring Boot 2.4.0 Breaks Legacy Applications

I'm upgrading a set of legacy applications to Spring Boot 2.4.0, running on OpenJDK 8, deployed to Pivotal Cloud Foundry.

The app and all its unit tests ran perfectly under Spring 2.3.4-RELEASE before I started this upgrade.

There was work to be done right off the bat: Spring Boot 2.4.0 brings in JUnit 5.x, so I had to fix all the JUnit 4.x tests to use the new annotations and classes.

After fixing all the tests I tried to run in IntelliJ 2020.2. All the tests failed, for the same reason: java.lang.IllegalStateException: Failed to load ApplicationContext. The root of the stack trace gives this cause:

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata

They removed a fundamental class from Spring Boot for the sake of Kubernetes, from the sound of it. I'm not sure I see why.

I did a Google search and found this explanation and fix.

I added spring.config.use-legacy-processing to my application.yml and a test application.properties file:

spring:
  config:
    use-legacy-processing: true

Still no joy - all the tests fail.

I'm confused by other links I've seen. Is this an issue with spring-cloud-dependencies? I'm reading that there might an issue with the Hoxton version. Do I need to add it to my app? It never needed this dependency before the upgrade:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

The problem is bigger than the tests. If I ignore them and try to run the app I still fail:

ERROR [main]: Application run failed |ApplicationName=Risk_Advisor | sourcedfrom=ERROR 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]

The root cause is the same:

java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
like image 817
duffymo Avatar asked Dec 09 '20 20:12

duffymo


People also ask

What version of spring does spring boot 2.4 use?

Spring Boot 2.4 uses Spring Framework 5.3.

How do I upgrade my spring boot?

To upgrade an existing CLI installation, use the appropriate package manager command (for example, brew upgrade ). If you manually installed the CLI, follow the standard instructions, remembering to update your PATH environment variable to remove any older references.

Which Java version should I use for spring boot application?

Although you can use Spring Boot with Java 6 or 7, we generally recommend Java 8 if at all possible.


2 Answers

when ever you do upgrades on spring, pls always look at the spring / cloud compatibility matrix and then upgrades the versions for both spring and cloud accordingly:

Also, pls note that you may not have added spring cloud as direct dependency but it may be coming into your final jar as a transitive dependency so its always better to add below spring cloud dependency as direct dependency management in your pom to stay away from any conflicts:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring.cloud-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
like image 196
Dean Jain Avatar answered Sep 30 '22 11:09

Dean Jain


I got the same error when migrating to Spring Boot 2.4.4 which forced me to migrate to Spring Cloud 2020.0.1 as well. In my case, the error was due to Bootstrap, provided by spring-cloud-commons, not being any longer enabled by default. I had to re-enabled it by adding the following new starter in my build.gradle file as stated here:

implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'

You say your app doesn't use Spring Cloud, so I'm not sure why you ended up facing which seems to be the same issue. Hopefully this workaround might work for you and help out others using Spring Cloud.


To fix the failing JUnit 4 tests, I have just added it as a dependency:

testImplementation 'junit:junit:4.13' 
like image 23
dbaltor Avatar answered Sep 30 '22 09:09

dbaltor