本文介绍了没有可用的“javax.sql.DataSource"类型的合格 bean:在候选对象中找到了多个“主要"bean:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置 Spring Boot 以使用 2 个 JNDI 数据源.我试过这个配置:

I want to configure Spring Boot to use 2 JNDI datasources. I tried this configuration:

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();
    }
}

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

When I deploy the code I get exception:

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

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

Do you know how I can solve this issue?

推荐答案

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

You can have only 1 primary Datasource. Other confuguration looks fine.

假设 ContextWarehouseDatasource 作为辅助连接

Assuming ContextWarehouseDatasource as secondary connection

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

Remove @Primary from warehouseDataSource() like this and try.

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

这篇关于没有可用的“javax.sql.DataSource"类型的合格 bean:在候选对象中找到了多个“主要"bean:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:53