【问题标题】:No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates:没有可用的“javax.sql.DataSource”类型的合格 bean:在候选中找到多个“主要”bean:
【发布时间】:2020-03-19 00:52:37
【问题描述】:

我想将 Spring Boot 配置为使用 2 个 JNDI 数据源。我试过这个配置:

application.properties

spring.production-datasource.jndi-name=java:/global/production_gateway
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

spring.warehouse-datasource.jndi-name=java:/global/production_warehouse
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

主数据库

@Configuration
@EnableJpaRepositories(
        basePackages = "org.datalis.plugin.production.entity", 
        entityManagerFactoryRef = "productionEntityManager", 
        transactionManagerRef = "productionTransactionManager"
    )
@EnableTransactionManagement
public class ContextProductionDatasource {

    @Autowired
    private Environment env;

    @Primary
    @Bean
    @ConfigurationProperties(prefix="spring.production-datasource")
    public DataSource productionDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean   
    public EntityManager productionEntityManager(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }

    @Primary
    @Bean
    public PlatformTransactionManager productionTransactionManager(final EntityManagerFactory emf) {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Primary
    @Bean
    public PersistenceExceptionTranslationPostProcessor productionExceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

第二个数据源:

@Configuration
@EnableJpaRepositories(
        basePackages = "org.datalis.plugin.warehouse.entity", 
        entityManagerFactoryRef = "warehouseEntityManager", 
        transactionManagerRef = "warehouseTransactionManager"
    )
@EnableTransactionManagement
public class ContextWarehouseDatasource {

    @Autowired
    private Environment env;

    @Primary
    @Bean
    @ConfigurationProperties(prefix="spring.warehouse-datasource")
    public DataSource warehouseDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean   
    public EntityManager warehouseEntityManager(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }

    @Bean
    public PlatformTransactionManager warehouseTransactionManager(final EntityManagerFactory emf) {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor warehouseExceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

当我部署代码时出现异常:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [productio                     nDataSource, warehouseDataSource]"}}

完整的错误堆栈:

https://pastebin.com/EsNp2Fp9

你知道我该如何解决这个问题吗?

【问题讨论】:

  • [Spring Data JPA - 多个 EnableJpaRepositories ](stackoverflow.com/questions/45663025/…) 的可能副本。根据那篇文章和我的知识,我相信您需要更改 EntityManager bean 实现以实现自定义 EntityManager bean 并在 bean 创建期间设置每个数据源。

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

您只能拥有 1 个主数据源。其他配置看起来不错。

假设ContextWarehouseDatasource作为辅助连接

像这样从warehouseDataSource() 中删除@Primary 并尝试。

@Bean
@ConfigurationProperties(prefix="spring.warehouse-datasource")
public DataSource warehouseDataSource() {
    return DataSourceBuilder.create().build();
}

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 2015-09-29
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2020-06-06
    • 2015-12-24
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多