spring 引导中的 Axon extensions.mongo 4.3:noSuchMethodError - com.mongodb.MongoClient

Axon extensions.mongo 4.3 in spring boot: noSuchMethodError - com.mongodb.MongoClient

我正在尝试 运行 mvn clean install 一个 spring 启动项目与 Axon 的 extensions.mongo 依赖关系。 mongo 数据库已启动并且 运行 正在 docker 容器中

在我的 pom 中:

<dependency>
    <groupId>org.axonframework.extensions.mongo</groupId>
    <artifactId>axon-mongo</artifactId>
    <version>4.3</version>
</dependency>

然后我有一个 @Configuration class 其中有 - 其中包括这些成员:

 @Value("${spring.data.mongodb.host:127.0.0.1}")
    private String mongoHost;

    @Value("${spring.data.mongodb.port:27017}")
    private int mongoPort;

    @Value("${spring.data.mongodb.database:user}")
    private String mongoDatabase;

    @Bean
    public MongoClient mongoClient() {
        var mongoFactory = new MongoFactory();
        mongoFactory.setMongoAddresses(Collections.singletonList(new ServerAddress(mongoHost, mongoPort)));

        return mongoFactory.createMongo();
    }

然后当 运行ning mvn clean install 我得到这个堆栈跟踪:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient' defined in com.springbank.user.core.configuration.AxonConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NoSuchMethodError: 'com.mongodb.connection.ConnectionPoolSettings$Builder com.mongodb.connection.ConnectionPoolSettings$Builder.maxWaitQueueSize(int)'
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongoClient' threw exception; nested exception is java.lang.NoSuchMethodError: 'com.mongodb.connection.ConnectionPoolSettings$Builder com.mongodb.connection.ConnectionPoolSettings$Builder.maxWaitQueueSize(int)'
Caused by: java.lang.NoSuchMethodError: 'com.mongodb.connection.ConnectionPoolSettings$Builder com.mongodb.connection.ConnectionPoolSettings$Builder.maxWaitQueueSize(int)'

Spring 当 MongoClient 在类路径中可用时,引导将自动配置它。您只需要在 application.properties 中指定正确的 spring.mongo 属性(根据您的配置判断,您已经使用了)。

所以简单的解决方法就是删除这个 @Bean 方法。

如果你真的想使用那个 MongoFactory 你需要弄清楚哪个版本的 MongoDB(在这种情况下是 Mongo 客户端)与其兼容。