春季未选择主要实体经理工厂
配置了两个数据源。主要EM工厂:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"com.example.data.postgresql"},
entityManagerFactoryRef = "postgreSqlEntityManagerFactory",
transactionManagerRef = "postgreSqlEntityTransactionManager"
)
public class PostgreSqlJpaConfig {
...
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean postgreSqlEntityManagerFactory(
@Qualifier("postgreSqlDataSource") DataSource dataSource,
EntityManagerFactoryBuilder builder){
return builder.dataSource(dataSource).packages("com.example.postgresql").build();
}
辅助EM Factory:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"com.example.data.greenplum"},
entityManagerFactoryRef = "greenPlumEntityManagerFactory",
transactionManagerRef = "greenPlumEntityTransactionManager"
)
public class GreenPlumJpaConfig {
@Bean
public LocalContainerEntityManagerFactoryBean greenPlumEntityManagerFactory(
@Qualifier("greenPlumDataSource") DataSource dataSource,
EntityManagerFactoryBuilder builder){
return builder.dataSource(dataSource).packages("com.example.greenplum").build();
}
服务在com.example.service
中。有时他们会使用实体管理器,而春天不能在两个之间进行选择:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field entityManager in ... required a single bean, but 2 were found:
我可以在每个EM注入中手动添加一个预选赛以使其正常工作,
@Autowired
public void setEntityManager(@Qualifier("postgreSqlEntityManagerFactory") EntityManager entityManager) {
this.entityManager = entityManager;
}
但是应用程序中有120多个EM注射告诉Spring在未明确指定的所有情况下使用默认主要EM。
There are two datasources configured. Primary em factory:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"com.example.data.postgresql"},
entityManagerFactoryRef = "postgreSqlEntityManagerFactory",
transactionManagerRef = "postgreSqlEntityTransactionManager"
)
public class PostgreSqlJpaConfig {
...
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean postgreSqlEntityManagerFactory(
@Qualifier("postgreSqlDataSource") DataSource dataSource,
EntityManagerFactoryBuilder builder){
return builder.dataSource(dataSource).packages("com.example.postgresql").build();
}
Secondary em factory:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"com.example.data.greenplum"},
entityManagerFactoryRef = "greenPlumEntityManagerFactory",
transactionManagerRef = "greenPlumEntityTransactionManager"
)
public class GreenPlumJpaConfig {
@Bean
public LocalContainerEntityManagerFactoryBean greenPlumEntityManagerFactory(
@Qualifier("greenPlumDataSource") DataSource dataSource,
EntityManagerFactoryBuilder builder){
return builder.dataSource(dataSource).packages("com.example.greenplum").build();
}
Services lays in com.example.service
. Sometimes they use entity manager, and Spring can't choose between two:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field entityManager in ... required a single bean, but 2 were found:
I can manually add a qualifier to the every em injection to make it work,
@Autowired
public void setEntityManager(@Qualifier("postgreSqlEntityManagerFactory") EntityManager entityManager) {
this.entityManager = entityManager;
}
but there are 120+ em injections in the app so I hope there should be another way to tell Spring to use the default primary em in all cases where it is not specified explicitly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是过时的春季靴子。升级到2.6.2解决了问题。
The problem was the outdated Spring Boot. Upgrade to 2.6.2 solved the issue.