mongodbspring-bootspring-framework-beans

Issue upgrading SpringBoot from 2.2.4 to 2.7.2 - Error creating bean with name 'mongoDatabaseFactory' defined in class path resource


I have a SpringBoot application with the following version. I plan to update spring-boot-starter-parent to 2.7.2.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
</parent>

When I change the version to 2.7.2 and run the code, I get the below error.

ERROR [main] TomcatStarter:61 onStartup - Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'beanConfiguration': Unsatisfied dependency expressed through field 'tokenServices'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TokenServices': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authProfileImpl': Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'mongoDatabaseFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory

I tried searching for the error messages over the internet, however, I couldn't figure out any solution.

I need suggestions on the steps to troubleshoot the issue in order to identify the solution.


Solution

  • NoClassDefFoundError typically means that you have some version conflict for some dependencies. In this case with com.mongodb.xxxx dependency.

    It looks like com.mongodb.connection.DefaultClusterFactory was moved from mongodb-driver into mongo-java-driver and according to @Sahil Khanna it fixes the issue, but, for spring projects, to avoid such issues in the future better to avoid direct dependencies to mongo and rely on standard Spring Data MongoDB dependencies managed by Spring BOM if possible Developing with Spring Boot - Dependency Management

    The curated list contains all the Spring modules that you can use with Spring Boot as well as a refined list of third party libraries. The list is available as a standard Bills of Materials (spring-boot-dependencies) that can be used with both Maven and Gradle.