Java SpringJPA数据:多个数据源和一个Spring配置
我正在尝试使用Spring jpa数据中的Crud存储库: 我的数据访问bean配置如下所示:Java SpringJPA数据:多个数据源和一个Spring配置,java,spring,jpa,spring-data-jpa,Java,Spring,Jpa,Spring Data Jpa,我正在尝试使用Spring jpa数据中的Crud存储库: 我的数据访问bean配置如下所示: @Configuration @EnableTransactionManagement @ComponentScan (basePackages = {"com.comp.olme"}) @PropertySource("classpath:OlmeSmb-${env}.properties") @EnableJpaRepositories(basePackages="com.comp.olme",
@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;
}
}
如您所见,我有两个数据源,两个EntityManagerFactory,两个TransactionManager和其他
但我只将一个EntityManagerFactory传递给@EnableJpaRepositories
注释(entityManagerFactoryRef=“emGapSort”
)
问题是:如何使用两个以上的数据源(entityManagerFactory)和SpringJPA数据
我阅读了一个示例,其中拆分配置被描述为一个解决方案,但我想使用一个Spring配置。可能吗?
谢谢。来自spring boot-
经过仔细考虑,我们决定不实施这项计划。您的注释建议是配置键前缀、bean前缀和其他bean相关设置的混合。虽然它在纸面上听起来很吸引人,但它很难实现,而且可能更难与用户的定制保持一致
因此,您可以尝试将其放在两个
@Configuration
类上(每个@Configuration
一个@EnableJpaRepositories
)。这是不可能的。您必须拆分为2个Java配置。尝试在配置中定义2@EnableJpaRepositories时,将出现编译错误。