我正在尝试使用Spring-jpa-data中的Crud Repository:
我的数据访问bean配置如下:
@Configuration
@EnableTransactionManagement
@ComponentScan (basePackages = {"com.comp.olme"})
@PropertySource("classpath:OlmeSmb-${env}.properties")
@EnableJpaRepositories(basePackages="com.comp.olme", entityManagerFactoryRef ="emGapSort", transactionManagerRef = "txManagerGapSort")
@EnableScheduling
public class OlmeSmbConfig {
@Bean
public BasicDataSource olmeDataSource() {
BasicDataSource olmeDataSource = new BasicDataSource();
...
return olmeDataSource;
}
@Bean
public BasicDataSource gapSortDataSource() {
BasicDataSource gapSortDataSource = new BasicDataSource();
...
return gapSortDataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean emFactoryOLME(@Qualifier("olmeDataSource") BasicDataSource olmeDataSource) {
LocalContainerEntityManagerFactoryBean localConnectionFactoryBean = new LocalContainerEntityManagerFactoryBean();
...
return localConnectionFactoryBean;
}
@Bean
public LocalContainerEntityManagerFactoryBean emFactoryGapSort(@Qualifier("gapSortDataSource") BasicDataSource gapSortDataSource) {
LocalContainerEntityManagerFactoryBean localConnectionFactoryBeanGapSort = new LocalContainerEntityManagerFactoryBean();
...
return localConnectionFactoryBeanGapSort;
}
@Bean
public EntityManager emOLME(@Qualifier("emFactoryOLME") EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
@Bean
public EntityManager emGapSort(@Qualifier("emFactoryGapSort") EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
@Bean
public JpaTransactionManager txManagerOLME(@Qualifier("emFactoryOLME") EntityManagerFactory entityManagerFactoryOLME) {
JpaTransactionManager txManagerOLME = new JpaTransactionManager();
...
return txManagerOLME;
}
@Bean
public JpaTransactionManager txManagerGapSort(@Qualifier("emFactoryGapSort") EntityManagerFactory entityManagerFactoryGapSort) {
JpaTransactionManager txManagerGapSort = new JpaTransactionManager();
....
return txManagerGapSort;
}
}
所以,正如你所看到的,我有两个数据源,两个EntityManagerFactories,两个TransactionManagers和其他...
但我只将一个EntityManagerFactory传递给@EnableJpaRepositories
注释(entityManagerFactoryRef ="emGapSort"
)。
问题是:两个如何使用多个DataSources(entityManagerFactory)和Spring-jpa-data?
我读了一个将分配配置描述为解决方案的示例,但我想使用一个Spring Config。可能吗? 谢谢。
答案 0 :(得分:1)
从春季开机 - #5401
经过仔细考虑后,我们决定不执行此操作。您的注释提议是配置键前缀,bean前缀和其他与bean相关的设置的混合。虽然它在纸面上听起来很吸引人,但它很难实现,甚至可能更难以使其与用户的自定义保持一致。
因此,您可以尝试将其放在两个@Configuration
个类上(每个@EnableJpaRepositories
一个@Configuration
)。