Using class-based and annotation-based configuration, you can split the configuration of each Spring Data JPA repository and its DataSource
into a separate configuration file.
This article has an example:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager",
basePackages = { "com.sctrcd.multidsdemo.integration.repositories.bar" })
public class BarConfig {
@Autowired JpaVendorAdapter jpaVendorAdapter;
@Bean(name = "barDataSource")
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setName("bardb").setType(EmbeddedDatabaseType.HSQL).build();
}
@Bean(name = "barEntityManager")
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}
...
}
Then you would do the same for FooConfig
, that is, for the other configuration class that will set another DataSource
, EntityManager
and @EnableJpaRepositories
.
Via XML the process would be identical. The important thing is that the EntityManager
and TransactionManager especificados em cada configuração de repositório apontem para o
DataSource 'desired.