我有一个现有的后端Java应用程序,并试图将其逐渐转换为SpringBoot应用程序。我从使用SpringBoot的父pom开始。
但是,在初始化Spring Context时,由于Mongo SpringBeans无法正确初始化而导致错误。接下来将显示特定的错误(但对于根本原因没有帮助)。
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'EmbeddedMongoServer'
defined in class path resource [
org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]:
Bean instantiation via factory method failed; nested exception...
添加了换行符以提高SO的可读性。
但是,现有应用程序代码不使用SpringMongo ,但是使用不带Spring的Mongo 。
所以如何防止Spring尝试自动配置Mongo? (又称Spring自动装配变得疯狂)。
我知道初始化失败是因为Spring的自动配置启动了,因为它在类路径上找到了com.mongodb.MongoClient
,并通过启用Spring调试发现了这一点(请参阅Is there a good way to diagnose spring bean/service creation problem?)。
启用Spring Debug之后,我会看到以下消息:
EmbeddedMongoAutoConfiguration matched:
- @ConditionalOnClass found required classes 'com.mongodb.MongoClient', 'de.flapdoodle.embed.mongo.MongoStarter'
所以我知道 Springs AutoConfiguration 是试图实例化Mongo Spring Bean的元凶,但不知道如何阻止它的发生。
我正在尝试将现有(非Spring)应用程序变成SpringBoot应用程序,并且正在使用SpringBoot父级和SpringWeb模块,如下所示。
<parent>
<groupId>org.springframework.boot</groupId>
<artifcatId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
我想使用Spring的RestTemplate
对其他服务器进行http休息调用,这就是为什么添加了对spring-web的依赖的原因。
在寻找解决方案时,我发现了以下内容,其中一些更有用。
帮助我启用调试来跟踪问题。 Is there a good way to diagnose spring bean/service creation problem?
有关自动装配的Spring文档。很高兴知道这一点。 https://docs.spring.io/spring/docs/5.0.0.M5/spring-framework-reference/htmlsingle/#beans-autowired-exceptions
如何从自动装配中排除bean。我希望这可能会有所帮助,但是我无法采取解决方案。 https://docs.spring.io/spring/docs/5.0.0.M5/spring-framework-reference/htmlsingle/#beans-factory-autowire-candidate
其他没有帮助的
那里有很多东西,所以我希望答案能从StackOverflow上的其他人那里得到。