skip to Main Content

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:

  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

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

2

Answers


  1. Chosen as BEST ANSWER

    After several tests of dependency versions, I found the dependencies that did not conflict with each other.
    Here's the list of versions used:

    1. Java JDK 14
    2. spring-boot-starter-parent:2.2.6.RELEASE
    3. axon-spring-boot-starter:4.3.1
    4. lombok:1.18.20

    Using other versions of spring boot exceptions occurred.


  2. I think you can just remove the MongoClient Bean, as Spring Boot is already doing that for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search