'I get exception with axon-mongo 4.5, spring boot 2.4.3 and mongodb 4.2 in AxonConfig class
When running the project I get this exception:
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]
this is my axon configuration 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();
}
...
}
The spring boot version and dependencies I use are:
- org.springframework.boot:spring-boot-starter-parent:2.4.3
- org.axonframework:axon-spring-boot-starter:4.5.9
- org.axonframework.extensions.mongo:axon-mongo:4.3
this is my 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
Solution 1:[1]
I think you can just remove the MongoClient Bean, as Spring Boot is already doing that for you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gerard Klijs |
