Don't create axonframework database

i don’t want to generate axonframework database, and use this.

private fun mongoTemplate(client: MongoClient): DefaultMongoTemplate {
        return DefaultMongoTemplate
            .builder()
            .mongoDatabase(client, 'test')
            .build()
    }

i can insert event to test database’s domainevents table but still axonframework database created.

how to stop creating axonframework database?

Could you please ad some more context? Seems like you want to run an integration test, but without using a database?

What would you want to use instead? If it’s indeed for an integration test, you can use testcontainers so the created database will only be used for the test.

@gklijs thanks for reply!!

it’s not for integration test, i’m studying about axon.

i just want to use my database. not ‘axonframework’ database.

i mean in mongodb, i make ‘test’ database and make read model here.
so i want to use only ‘test’ not ‘axonframework’

but if i start application, ‘axonframework’ database is auto created.

You can set the database name with the
builder.

@gklijs thank you to recommand refference!

yes, i’m already using builder so i can make ‘test’ database.

but i don’t want to see ‘axonframework’ database. it still auto created.

i only need ‘test’ database but ‘test’ and ‘axonframework’ databases are exist!

what’s wrong with it?

i guess before builder working, axonconfig(?) thing make already ‘axonframework’.

I think you confuse two builders. You need the one specific for the one in the Axon extension, otherwise it will indeed use the default.

@gklijs thank you to quick reply!

you are right im confusing now.

you said two builders, one is from DefaultMongoTemplate(like im using above) and the other is where can i find?

On second sight, it might be because you are using a private method, and don’t have the @Bean annotation, so the DefaultMongoTemplate you defined isn’t injected. It probably should also be of type AbstractMongoTemplate assuming you use Spring Boot.

@gklijs thank you to point out @Bean and public.

if i use below code, i can see error The bean 'mongoTemplate', defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [fileresource path] and overriding is disabled.

@Bean
    fun eventStore(
        storageEngine: EventStorageEngine,
        configuration: AxonConfiguration
    ): EmbeddedEventStore {
        return EmbeddedEventStore.builder()
            .storageEngine(storageEngine)
            .messageMonitor(configuration.messageMonitor(EventStore::class.java, "eventStore"))
            .build()
    }

@Bean
    fun storageEngine(client: MongoClient): EventStorageEngine {
        return MongoEventStorageEngine
            .builder()
            .mongoTemplate(this.mongoTemplate(client))
            .build()
    }

@Bean
fun mongoTemplate(client: MongoClient): DefaultMongoTemplate {
        return DefaultMongoTemplate
            .builder()
            .mongoDatabase(client, 'test')
            .build()
    }

after i set spring.main.allow-bean-definition-overriding=true, it’s still not work

@imnewone,

if you make your MongoTemplate bean also @Primary, does it work?

KR,

@lfgcampos thank you to say @Primary

below code is my whole config code.
i still have error Parameter 0 of method storageEngine in com.cqrs.axon.AxonConfig required a bean of type 'org.axonframework.extensions.mongo.DefaultMongoTemplate' that could not be found. Consider defining a bean of type 'org.axonframework.extensions.mongo.DefaultMongoTemplate' in your configuration.

@Bean
fun eventStore(
    storageEngine: EventStorageEngine,
    configuration: AxonConfiguration
): EmbeddedEventStore {
    return EmbeddedEventStore.builder()
        .storageEngine(storageEngine)
        .messageMonitor(configuration.messageMonitor(EventStore::class.java, "eventStore"))
        .build()
}

@Bean
@Primary
fun mongoTemplate(client: MongoClient): DefaultMongoTemplate {
    return DefaultMongoTemplate
        .builder()
        .mongoDatabase(client, "test")
        .build()
}

// The `MongoEventStorageEngine` stores each event in a separate MongoDB document
@Bean
fun storageEngine(template: DefaultMongoTemplate): EventStorageEngine {
    return MongoEventStorageEngine
        .builder()
        .mongoTemplate(template)
        .snapshotSerializer(this.serializer())
        .eventSerializer(this.serializer())
        .build()
}

@Bean
@Primary
fun serializer(): Serializer {
    return JacksonSerializer
        .builder()
        .objectMapper(
            jacksonObjectMapper()
                .registerModule(KotlinModule())
                .registerModule(Jdk8Module())
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
        )
        .build()
}