Spring Boot handle multiple MongoTemplates for Multi Cluster MongoDB

I have to implement multi-tenancy using spring-boot, mongodb. Previously I was using same cluster so things were simple enough. Now each tenant would have its own database in its own cluster. So I was thinking of having multiple mongoTemplates managed in a single application.

My requirements:

  1. I wanted all Spring configuration in Java code/annotations. (I hate XML!)
  2. I wanted Spring to manage both the Mongo and MongoTemplate objects.
  3. I wanted to use MongoRepository Interface beans and have the correct MongoTemplate back it.

Also I am looking for that once the request comes in ( I am already getting client ID ) then correct mongoTemplate is injected to be used throughout application.

Could anyone provide some sample code or working example for this? Any help is greatly appreciated. Thanks!


did you find any solution for this? I’m looking too