title: Set @Repository to work with specific DataSource Spring-boot
body: <p>I have 2 DataSources defined in my Spring-boot Project and I also have 2 @Repository.</p>
  
  <p>I have this code in my </p>
  
  <blockquote>
    <p>Application.java</p>
  </blockquote>
  
  <pre><code>@SpringBootApplication
  @EnableJpaRepositories(basePackages = {"com.opes.kyc_adapter.repository.postgresql", "com.opes.kyc_adapter.repository.mysql"})
  @EntityScan(basePackages = {"com.opes.kyc_adapter.model.postgresql", "com.opes.kyc_adapter.model.mysql"})
  public class Application implements CommandLineRunner {
  
      @Autowired
      private MessageRepositoryMySQL repositoryMySQL;
  
      @Autowired
      private MessageRepositoryPostgreSQL repositoryPostgreSQL;
  
  
      public static void main(String[] args) {
  
          SpringApplication.run(Application.class, args);
  
      }
  
      @Override
      public void run(String... arg0) throws Exception {
          for (int i = 0; i &lt; 5; i++){
              repositoryMySQL.save(new MessageOriginal("My post number #" + (i+1)));
              repositoryPostgreSQL.save(new MessageExtracted("My post number #" + (i+1), "User #" + (i+1)));
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>As you can see I have 2 @Repository</p>
  
  <p>What happens is that each repo is supposed to add data to a different DataSource, but both are adding to the MySQL Datasouce.</p>
  
  <p>Here follows the rest of the code:</p>
  
  <blockquote>
    <p>DataBaseConfigurationMySQL.java</p>
  </blockquote>
  
  <pre><code>@Configuration
  public class DataBaseConfigurationMySQL implements EnvironmentAware {
  
      private RelaxedPropertyResolver jpaPropertyResolver;
  
      @Autowired(required = false)
      private PersistenceUnitManager persistenceUnitManager;
  
      @Override
      public void setEnvironment(Environment environment) {
          this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, "spring.jpa.");
      }
  
      @Bean
      @DependsOn("jdbcTemplate")
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
          if (persistenceUnitManager != null) {
              entityManagerFactoryBean
                      .setPersistenceUnitManager(persistenceUnitManager);
          }
          entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
          entityManagerFactoryBean.setDataSource(dataSource);
          entityManagerFactoryBean.setPackagesToScan("com.opes.kyc_adapter.model.mysql");
          entityManagerFactoryBean.getJpaPropertyMap().putAll(jpaPropertyResolver.getSubProperties("properties."));
          Map&lt;String, Object&gt; properties = entityManagerFactoryBean.getJpaPropertyMap();
          properties.put("hibernate.ejb.naming_strategy", jpaPropertyResolver.getProperty("hibernate.naming-strategy", SpringNamingStrategy.class.getName()));
          properties.put("hibernate.hbm2ddl.auto", jpaPropertyResolver.getProperty("hibernate.ddl-auto", "create-drop"));
          return entityManagerFactoryBean;
      }
  
      @Bean
      public DataSource dataSource(){
          return DataSourceBuilder.create()
                  .url("jdbc:mysql://localhost:3306/kyc-adapter-mysql")
                  .driverClassName("com.mysql.jdbc.Driver")
                  .username("root")
                  .password("pass")
                  .build();
      }   
  
  
  
  
  }
  </code></pre>
  
  <blockquote>
    <p>DataBaseConigurationPostgreSQL.java</p>
  </blockquote>
  
  <pre><code>@Configuration
  public class DataBaseConfigurationPostgreSQL implements EnvironmentAware {
  
      private RelaxedPropertyResolver jpaPropertyResolver;
  
      @Autowired(required = false)
      private PersistenceUnitManager persistenceUnitManager;
  
      @Override
      public void setEnvironment(Environment environment) {
          this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, "spring.jpa.");
      }
  
      @Bean
      @DependsOn("jdbcTemplate")
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
          if (persistenceUnitManager != null) {
              entityManagerFactoryBean
                      .setPersistenceUnitManager(persistenceUnitManager);
          }
          entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
          entityManagerFactoryBean.setDataSource(dataSource);
          entityManagerFactoryBean.setPackagesToScan("com.opes.kyc_adapter.model.postgresql");
          entityManagerFactoryBean.getJpaPropertyMap().putAll(jpaPropertyResolver.getSubProperties("properties."));
          Map&lt;String, Object&gt; properties = entityManagerFactoryBean.getJpaPropertyMap();
          properties.put("hibernate.ejb.naming_strategy", jpaPropertyResolver.getProperty("hibernate.naming-strategy", SpringNamingStrategy.class.getName()));
          properties.put("hibernate.hbm2ddl.auto", jpaPropertyResolver.getProperty("hibernate.ddl-auto", "create-drop"));
          return entityManagerFactoryBean;
      }
  
      @Bean
      public DataSource dataSource(){
          return DataSourceBuilder.create()
                  .url("jdbc:postgresql://localhost:5432/kyc-adapter-postgresql")
                  .driverClassName("org.postgresql.Driver")
                  .username("postgres")
                  .password("pass")
                  .build();
      }   
  </code></pre>
  
  <blockquote>
    <p>MessageOriginal.java</p>
  </blockquote>
  
  <pre><code>@Entity
  public class MessageOriginal {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
  
      private String message;
  
      public MessageOriginal(){
  
      }
  
      public MessageOriginal(String message){
          this.message = message;     
      }
  
      //getters and setters
  }
  </code></pre>
  
  <blockquote>
    <p>MessageExtracted.java</p>
  </blockquote>
  
  <pre><code>@Entity
  public class MessageExtracted {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
  
      private String message;
  
      private String userName;
  
      public MessageExtracted(){
  
      }
  
      public MessageExtracted(String message, String userName){
          this.message = message;
          this.userName = userName;
      }
  
      //getters and setters
  }
  </code></pre>
  
  <blockquote>
    <p>MessageRepositoryMySQL.java</p>
  </blockquote>
  
  <pre><code>@Repository
  public interface MessageRepositoryMySQL extends CrudRepository&lt;MessageOriginal, Long&gt;{
  
  }
  </code></pre>
  
  <blockquote>
    <p>MessageRepositoryPostgreSQL.java</p>
  </blockquote>
  
  <pre><code>@Repository
  public interface MessageRepositoryPostgreSQL extends CrudRepository&lt;MessageExtracted, Long&gt;{
  
  }
  </code></pre>