[英]How do I configure multiple JPA data sources using Spring @Configuration class?
[英]With Spring boot and multiple data sources how do I specify @primary outside the configuration class?
我正在使用庫中定義的2個數據源,因為配置是在多個項目之間共享的。
我收到這個錯誤
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
指定哪個數據源是@Primary有點麻煩。 我真的很想將特定的實體管理器映射到特定的存儲庫。
jar文件中定義的兩個配置文件都相同。 將第二種配置的jr更改為jr2。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "jrEntityManagerFactory",
transactionManagerRef = "jrTransactionManager",
basePackages = {"au.com.jr"}
)
public class PSPTrxDatasourceConfig {
public PSPTrxDatasourceConfig() {
}
@Bean(
name = {"jrDataSource"}
)
@ConfigurationProperties(
prefix = "jr.datasource"
)
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(
name = {"jrEntityManagerFactory"}
)
public LocalContainerEntityManagerFactoryBean jrEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("jrDataSource") DataSource jrDataSource) {
return builder.dataSource(jrDataSource).packages(new String[]{"au.com.jr"}).persistenceUnit("jr").build();
}
@Bean(
name = {"jrTransactionManager"}
)
public PlatformTransactionManager transactionManager(@Qualifier("jrEntityManagerFactory") EntityManagerFactory jrEntityManagerFactory) {
return new JpaTransactionManager(jrEntityManagerFactory);
}
}
我們創建了一個hack。 還有一個額外的“多余”數據源在本地項目中指定@Primary。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.