Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Multiple MongoDB repositories with Spring Data Mongo

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

like image 625
Miciurash Avatar asked Jul 28 '15 16:07

Miciurash


People also ask

What is the difference between MongoOperations and MongoTemplate?

MongoTemplate provides a simple way for you to save, update, and delete your domain objects and map those objects to documents stored in MongoDB. You can save, update and delete the object as shown below. MongoOperations is the interface that MongoTemplate implements.

How does spring boot connect to multiple databases?

Multiple Databases in Spring Boot The interesting part is annotating the data source bean creation method with @ConfigurationProperties. We just need to specify the corresponding config prefix. Inside this method, we're using a DataSourceBuilder, and Spring Boot will automatically take care of the rest.

Can MongoDB have multiple databases?

mongoDB database : A physical container for collections. [where as in RDBMS it's a collection of related tables] Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.


1 Answers

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.

like image 60
Ori Dar Avatar answered Oct 13 '22 17:10

Ori Dar