扩展 QueryDslRepositorySupport 和多个数据源时获取 NoUniqueBeanDefinitionException

Getting NoUniqueBeanDefinitionException when extends QueryDslRepositorySupport and multiple datasources

我需要使用多个数据库。 我正在使用 Spring Boot + Spring Data JPA, 所以我有两个配置 类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.rest.dao.first",
        entityManagerFactoryRef = "firstEntityManagerFactory", transactionManagerRef = "firstTransactionManager")
public static class DnbbJdbcConfig {

    @Primary
    @Bean
    @ConfigurationProperties(prefix="datasource.first")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "firstEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSource())
                .packages("com.rest.dao.first")
                .persistenceUnit("first")
                .build();
    }

    @Primary
    @Bean(name = "firstTransactionManager")
    PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactory(builder).getObject());
    }

}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.rest.dao.second",
    entityManagerFactoryRef = "secondEntityManagerFactory", transactionManagerRef = "secondTransactionManager")
public static class SmsJdbcConfig {

    @Bean
    @ConfigurationProperties(prefix="datasource.second")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondEntityManagerFactory")
    @PersistenceContext(unitName = "second")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSource())
                .packages("com.rest.dao.second")
                .persistenceUnit("second")
                .build();
    }

    @Bean(name = "secondTransactionManager")
    PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactory(builder).getObject());
    }
}

我猜没有错,是正确的。 所以,当我使用默认存储库时,不会出错。 (例如 userRepository.findById() - 多数据源中没有错误)

但是,当我使用自定义存储库时出现错误。 (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations)

自定义工具

public class FirstRepositoryImpl extends QueryDslRepositorySupport implements FirstCustomRepository {

public FirstRepositoryImpl() {
    super(First.class);
}

@PersistenceContext(unitName = "first")
private EntityManager entityManager;

private QFirst first = QFirst.first;

@Override
public List<String> messages() {
    JPAQuery query = new JPAQuery(entityManager);
    return query.from(first).list(first.message);
}   
}

ExceptionTrace

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: firstEntityManagerFactory,secondEntityManagerFactory
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:582) ~[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:541) ~[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:707) ~[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:680) ~[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:178) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354) ~[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE]
... 45 common frames omitted

我是不是配置有误?

我在我的 github 存储库中编写了示例代码.. https://github.com/okihouse/spring-boot-multiple-datasource-with-querydsl

自己解决了:

我正在检查 QueryDslRepositorySupport.class 并找到了。

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    Assert.notNull(entityManager);
    this.querydsl = new Querydsl(entityManager, builder);
    this.entityManager = entityManager;
}

@PersistenceContext 没有“unitName” 所以,Spring 无法注入 EntityManager。

我创造QueryDslRepositorySupportWrapper.java 并手动注入 EntityManager。

而且有效。

https://github.com/okihouse/spring-boot-multiple-datasource-with-querydsl