java - 带有 Morphia 配置的 Spring Boot:BeanCreationException
问题描述
我和我的项目团队使用 Morphia 创建了一个应用程序。现在我们想用 Spring 构建一个 Web 服务,它使用相同的 Entity-Classes 和 Wrappers。但似乎 Spring 试图从 morphia 库中实例化一个 Bean。
我的 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.mongodb.morphia</groupId>
<artifactId>morphia</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
我的主要课程:
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
启动时抛出以下错误
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.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalArgumentException: clusterListener can not be null
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
... lines omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalArgumentException: clusterListener can not be null
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
... lines omitted
一旦我添加了 morphia 依赖项,就会在启动时引发错误。
我必须如何配置 Spring Boot 才能解决此问题?
先感谢您
解决方案
似乎 Spring 尝试自动配置 MongoDb 驱动程序。我设法使用以下配置注释禁用自动配置:
@Configuration
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
public class MainApplication {}
推荐阅读
- c++ - 将 void * 传递给接受 const void * 的函数时,gcc 给了我可能未初始化的警告
- node.js - “错误 [GUILD_VOICE_CHANNEL_RESOLVE]:无法将频道解析为公会语音频道。” 使用 .setChannel() 时
- python - Tkinter:使用 2 个输入框中的百分比解决数学问题
- javascript - 如何在 Angular 中定义嵌套对象模型?
- java - 为什么我的返回值在 Java 中的对象中会自行增加?
- javascript - TypeError:无法读取空错误的属性“addEventListener”
- vue.js - 获取在第三次 nuxt-link 单击而不是第二次 nuxt-link 单击后填充的 vuex 存储
- uibutton - 斯威夫特 5 | 如何将附加参数传递给选择器@objc
- python - 有没有办法解决这个错误需要一些好的见解
- android - 您如何将 Compose LazyColumn 与 Coroutines/Room 数据库一起使用?