title: How to dynamically wire up Repositories with custom DataSources?
body: <p>So I have a fairly standard Spring Boot application using JavaConfig and JPA to wire up services and repositories. However, one non-standard aspect of the application is a requirement to spin up isolated cloud databases on demand to keep client data separate for legal reasons.</p>
  
  <p>I have a simple ClientService with some Repositories injected, and my goal is to create some sort of factory where I can request a version of this ClientService, specific to each client, that has a custom DataSource injected into all of the repositories. Spring Boot, which is normally wonderful for wiring things up, has made things more confusing because its hard to see whats going on behind the scenes.</p>
  
  <p>What would be the best way to approach this? My first idea is a bean called ClientServiceFactory with a method getClientService(Client client). Its easy enough for me to create the custom DataSource for this client - the hard part is how I can return an instance of ClientService, with all of the other things automatically injected, but forcing all the repository beans to use this datasource. Naturally ClientService would no longer be a singleton, but instead I would store a map internally of Client > ClientService.</p>
  
  <p>Any help or advice would be greatly appreciated.</p>