Configure Multiple MongoDB repositories with Spring Data Mongo

11,182

The base idea is to separate the package hierarchy that contains your repositories into two different paths:

  • com.whatever.repositories.main package for the main db repository interfaces
  • com.whatever.repositories.secondary package for the other db repository interfaces

Your XML configuration should be something such as:

<mongo:repositories base-package="com.whatever.repositories.main" mongo-template-ref="mongoTemplate"/>
<mongo:repositories base-package="com.whatever.repositories.secondary" mongo-template-ref="mongoAppTemplate"/>

EDIT

@EnableMongoRepositories annotation is not @Repeatable, but you can have two @Configuration classes, each annotated with @EnableMongoRepositories in order to achieve the same using annotations:

@Configuration
@EnableMongoRepositories(basePackages = "com.whatever.repositories.main", mongoTemplateRef = "mongoTemplate")
public class MainMongoConfig {
    ....
}

@Configuration
@EnableMongoRepositories(basePackages = "com.whatever.repositories.secondary", mongoTemplateRef = "mongoAppTemplate")
public class SecondaryMongoConfig {
    ....
}

And a third @Configuration annotated class which @Import the other two.

Share:
11,182
Miciurash
Author by

Miciurash

Updated on July 19, 2022

Comments

  • Miciurash
    Miciurash almost 2 years

    I have 2 Mongodb databases connected to a Spring Boot app with 2 MongoTemplate-s:

    mongoTemplate (the default bean name, connects to default db)

    mongoAppTemplate (connects to another database on run-time)

    I have a lot of MongoRepository-s that use mongoTemplate but I also want to create some that would use mongoAppTemplate.

    How can I configure 2 MongoRepository-s to use different MongoTemplate -s with Java configuration ?

    I found a way to do it with XML (link below), but I really want to keep it all annotation based

    Spring-data-mongodb connect to multiple databases in one Mongo instance

  • Miciurash
    Miciurash almost 9 years
    Can the same result be achieved through annotations?
  • Camilo Crespo
    Camilo Crespo over 8 years
    @orid Wondering if this approach is taken, adding a third @Configuration annotated class is redundant? Otherwise skipping the @Configuration annotation in the first two classes and then consolidating into a third class
  • s1moner3d
    s1moner3d over 8 years
    How to dynamically configure it? I mean, I have a list of templates, and want to use one of them based on certain conditions.
  • JaskeyLam
    JaskeyLam almost 7 years
    Then at last, how to inject a MongoRepository instance to my service? would you please compete the whole sample? Thank you!
  • devanathan
    devanathan over 6 years
    Hi @OriDar, Is there any way to switch between the two mongo database in the same instance via repository pattern?
  • heez
    heez over 5 years
    Super important note here: The MongoRepository interfaces MUST be in different packages. I also didn't need a 3rd class.