How to set the mongo db path when using axon-mongo


I’m trying to use Mongo db as the repository for events, and through the doc, there’s a MongoEventStorageEngine is provided in the module axon-mongo.
Here’s my configure class

public class AxonConfiguration {

    private String mongoUrl;

    private String mongoDbName;

    private String eventsCollectionName;

    private String snapshotCollectionName;

    Serializer axonJsonSerializer() {
        return new JacksonSerializer();

    public EventStorageEngine eventStore(){
        return new MongoEventStorageEngine(
                axonJsonSerializer(),null, axonMongoTemplate(), new DocumentPerEventStorageStrategy());

    @Bean(name = "axonMongoTemplate")
    public MongoTemplate axonMongoTemplate() {
        MongoFactory mongoFactory = new MongoFactory();
        mongoFactory.setMongoAddresses(Arrays.asList(new ServerAddress(mongoUrl)));
        MongoClient mongoClient = mongoFactory.createMongo();
        MongoTemplate template = new DefaultMongoTemplate(mongoClient, mongoDbName, eventsCollectionName, snapshotCollectionName);
        return template;


It’s actually working but in the console, I found there are two mongo connection, one is to the path I provided, and another is trying to connect localhost.
It seems after introducing this module, Spring will automatically do some initializing work to connect to a mongo db at localhost.
How can I make it avoid connecting localhost? Or can I configure the path to connect for the auto initialization?


It turns out that extracting the MongoClient initialization part into a method will solve this.
Clearly, Axon will check the every existence of a Bean returning MongoClient, and it will create a new one from the MongoFactory class with default config -> localhost.

But I didn’t find the class doing this work in the axon-mongo module, even in the AxonAutoConfiguration of the axon-spring-boot-autoconfigure module.

在 2017年3月14日星期二 UTC+8下午2:49:46,Edison Xu写道:

It’s not Axon trying to connect to Mongo, but Spring Boot AutoConfiguration, which detects a Mongo library.

thanks for clearing.

在 2017年3月14日星期二 UTC+8下午4:41:14,Allard Buijze写道: