当前位置: 首页 > 知识库问答 >
问题:

AxonConfig类中的axon-mongo 4.5、spring boot 2.4.3和mongodb 4.2出现异常

闻人宝
2023-03-14

在运行项目时,我得到以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'axonMongoTemplate' defined in com.springbank.user.core.configuration.AxonConfig: Bean 
instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.axonframework.extensions.mongo.MongoTemplate]: Factory method 'axonMongoTemplate' 
threw exception; nested exception is java.lang.IllegalStateException: @Bean method 
AxonConfig.mongo called as bean reference for type [com.mongodb.MongoClient] but overridden
 by non-compatible bean instance of type [com.mongodb.client.internal.MongoClientImpl].
 Overriding bean of same name declared in: class path resource 
[org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.axonframework.extensions.mongo.MongoTemplate]: Factory method 'axonMongoTemplate' 
threw exception; nested exception is java.lang.IllegalStateException: @Bean method 
AxonConfig.mongo called as bean reference for type [com.mongodb.MongoClient] but overridden
 by non-compatible bean instance of type [com.mongodb.client.internal.MongoClientImpl].
 Overriding bean of same name declared in: class path resource
 [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]

Caused by: java.lang.IllegalStateException: @Bean method AxonConfig.mongo called as bean 
reference for type [com.mongodb.MongoClient] but overridden by non-compatible bean instance 
of type [com.mongodb.client.internal.MongoClientImpl]. Overriding bean of same name declared 
in: class path resource 
[org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]

这是我的轴突配置类:

@Configuration
public class AxonConfig {

    @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 mongo() {
        var mongoFactory = new MongoFactory();
        mongoFactory.setMongoAddresses(Collections.singletonList(new ServerAddress(mongoHost, mongoPort)));

        return mongoFactory.createMongo();
    }

    @Bean
    public MongoTemplate axonMongoTemplate() {
        return DefaultMongoTemplate.builder()
                .mongoDatabase(mongo(), mongoDatabase)
                .build();
    }
...
}

我使用的Spring Boot版本和依赖项是:

  1. org.springframework.boot:spring-boot-starter-parent:2.4.3
  2. org.axonframework:axon-spring-boot-starter:4.5.9
  3. org.axonframework.extensions.mongo:axon-mongo:4.3

这是我的application.properties:

#spring
server.port=8082

#mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=user

spring.main.allow-bean-definition-overriding=true
spring.main.allow-circular-references=true

共有2个答案

公西凯捷
2023-03-14

在对依赖版本进行了几次测试后,我发现了彼此不冲突的依赖项。
以下是使用的版本列表:

    < li>Java JDK 14版 < Li > spring-boot-starter-parent:2 . 2 . 6 . release < Li > axon-spring-boot-starter:4 . 3 . 1 < li >龙目岛:1.18.20

使用其他版本的spring boot时出现异常。

劳仲渊
2023-03-14

我认为你可以删除MongoClient Bean,因为Spring Boot已经为你做了。

 类似资料:
  • 我正在使用第三方dll集成zteco生物识别设备。我通过使用tlbimp.exe并复制它来添加zkemkeeper.dll。 这是我的连接类 命名空间RTEvents{class Connection{public static string IP=“192.168.1.3”;public static string PORT=“4370”;public static string MACHINE

  • 我正在写一个任务,将文件从一个公共文件夹中的特定位置解压,如下所示 现在,当我执行任务时,它会给我NullPointerException,而没有其他细节。我不知道还需要什么。 这是我在stackTrace中得到的信息: 原因:java。org上的lang.NullPointerException。格雷德尔。应用程序编程接口。内部的文件IdentityFileResolver。doResolve(

  • 我在线程“main”java中遇到异常。错误:未解析编译。 我做错了什么? 线程“main”中出现异常: java.lang.错误:未解决的编译问题: 对于类型在

  • 我是Java公司的JSON新手,并试图使用maven和Eclipse来探索它。 这是我的JSON文件。 我的pom.xml文件如下所示 我试图列出所有的jsonp事件。下面是相同的代码段。 运行上述文件后,我得到以下错误。 谁能帮我找出哪里不对劲。

  • 问题内容: 我试图在Netbeans中重构一个大型程序,但我有点迷茫。我从来没有非常模块化,但是现在通过实际学习如何做到这一点来尝试纠正这种情况,并在将来纠正这种情况。不幸的是,我在将某些教程翻译成我的程序时遇到了麻烦。所以我希望这里有人可以帮忙。目前,我正在尝试分解一部分采用特定格式的文件并制成表格的代码。我知道我需要创建一个类并使用它来创建表对象,但是我不确定如何做。我有一个主文件,用于获取文

  • 问题内容: 返回-1000作为输出。 引发异常。 我如何能够识别正号之前的“ +”号而不必修整符号? 问题答案: 尝试使用该模式。它将处理显式签名解析。模式细分: 第一部分(在之前)是肯定模式,它必须以字符开头 第二部分是负数,必须以char 开头 例: 输出: