多个数据源和多个配置文件

时间:2019-06-18 17:05:17

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

由于我使用两个数据源,因此我有两个配置文件(“ autoContido”和“ weblogic”),并且每个配置文件都有两个配置类。

我已经将来自特定数据源的bean注释为@Primary,而来自其他数据源配置类的bean不是@Primary,但是我给它们命名了不同。

我认为通过使用@Primary批注不会像下面的那样出现错误,但是我仍然会得到它们。谁能帮我看看问题出在哪里?

"Parameter 0 of constructor in br.com.brb.maf.model.repository.impl.EmprestimoTelebancoRepositoryImpl required a single bean, but 2 were found:
    - org.springframework.orm.jpa.SharedEntityManagerCreator#0: defined by method 'createSharedEntityManager' in null
    - org.springframework.orm.jpa.SharedEntityManagerCreator#1: defined by method 'createSharedEntityManager' in null"

我尝试使用@Primary批注,但在br.com.brb.maf.model.repository.impl.EmprestimoTelebancoRepositoryImpl中仍然需要构造函数的错误参数0,但找到了两个bean

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"br.com.brb.maf.model.pesquisa.repository"},
        transactionManagerRef = "customMobileTransactionManager",
        entityManagerFactoryRef = "mobileEntityManager")
@Profile("weblogic")
public class BanknetDatabaseConfiguration implements EnvironmentAware {

 //ommited ...

@Value("${spring.datasource.mobile.jndi-name}")
    private String mobileJndiName;

    @Override
    public void setEnvironment(Environment environment) {
        this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, SPRING_JPA_PROPERTIES);
        this.dataSourcePropertyResolver = new RelaxedPropertyResolver(environment, SPRING_DATASOURCE);
    }

    @Bean(name = "customMobileTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("mobileEntityManager") EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean(name = "mobileDataSource")
    public DataSource dataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        DataSource dataSource = dsLookup.getDataSource(mobileJndiName);
        return dataSource;
    }

    @Bean(name = "mobileEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            @Qualifier("mobileDatasource") DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        em.setPackagesToScan(new String[] {"br.com.brb.maf.model.pesquisa"});

        return em;
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty(HIBERNATE_HBM2DDL_AUTO,
                jpaPropertyResolver.getProperty(HIBERNATE_HBM2DDL_AUTO, VALIDATE));
        properties.setProperty(HIBERNATE_DIALECT, jpaPropertyResolver.getProperty(HIBERNATE_DIALECT));
        properties.setProperty(HIBERNATE_DEFAULT_SCHEMA, jpaPropertyResolver.getProperty(HIBERNATE_DEFAULT_SCHEMA));
        properties.setProperty(HIBERNATE_SHOW_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_SHOW_SQL, Boolean.FALSE.toString()));
        properties.setProperty(HIBERNATE_FORMAT_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_FORMAT_SQL, Boolean.FALSE.toString()));
        return properties;
    }
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"br.com.brb.maf.model.repository"},
        transactionManagerRef = "customTransactionManager",
        entityManagerFactoryRef = "entityManagerFactory")
@Profile("weblogic")
public class DatabaseConfiguration implements EnvironmentAware {

//ommited...

@Value("${spring.datasource.maf.jndi-name}")
    private String mafJndiName;

    @Override
    public void setEnvironment(Environment environment) {
        this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, SPRING_JPA_PROPERTIES);
        this.dataSourcePropertyResolver = new RelaxedPropertyResolver(environment, SPRING_DATASOURCE);
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            @Qualifier("dataSource") DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        em.setPackagesToScan(new String[] {"br.com.brb.maf.model"});

        return em;
    }

    @Primary
    @Bean(name = "dataSource")
    public DataSource dataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        // DataSource dataSource =
        // dsLookup.getDataSource(dataSourcePropertyResolver.getProperty(JNDI_NAME));
        DataSource dataSource = dsLookup.getDataSource(mafJndiName);
        return dataSource;
    }

    @Primary
    @Bean(name = "customTransactionManager")
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

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

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty(HIBERNATE_HBM2DDL_AUTO,
                jpaPropertyResolver.getProperty(HIBERNATE_HBM2DDL_AUTO, VALIDATE));
        properties.setProperty(HIBERNATE_DIALECT, jpaPropertyResolver.getProperty(HIBERNATE_DIALECT));
        properties.setProperty(HIBERNATE_DEFAULT_SCHEMA, jpaPropertyResolver.getProperty(HIBERNATE_DEFAULT_SCHEMA));
        properties.setProperty(HIBERNATE_SHOW_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_SHOW_SQL, Boolean.FALSE.toString()));
        properties.setProperty(HIBERNATE_FORMAT_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_FORMAT_SQL, Boolean.FALSE.toString()));
        return properties;
    }
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = { "br.com.brb.maf.model.pesquisa.repository" })
@Profile("autoContido")
public class BanknetDatabaseConfigurationStandalone implements EnvironmentAware {

//ommited...

@Override
    public void setEnvironment(Environment environment) {
        this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, SPRING_JPA_PROPERTIES);
        new RelaxedPropertyResolver(environment, SPRING_DATASOURCE);
    }

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

    @Bean(name = "mobileEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            @Qualifier("mobileDataSource") DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        em.setPackagesToScan(new String[] {"br.com.brb.maf.model.pesquisa"});

        return em;
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty(HIBERNATE_HBM2DDL_AUTO,
                jpaPropertyResolver.getProperty(HIBERNATE_HBM2DDL_AUTO, VALIDATE));
        properties.setProperty(HIBERNATE_DIALECT, jpaPropertyResolver.getProperty(HIBERNATE_DIALECT));
        properties.setProperty(HIBERNATE_DEFAULT_SCHEMA, jpaPropertyResolver.getProperty(HIBERNATE_DEFAULT_SCHEMA));
        properties.setProperty(HIBERNATE_SHOW_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_SHOW_SQL, Boolean.FALSE.toString()));
        properties.setProperty(HIBERNATE_FORMAT_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_FORMAT_SQL, Boolean.FALSE.toString()));
        return properties;
    }
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = { "br.com.brb.maf.model.repository" })
@Profile("autoContido")
public class DatabaseConfigurationStandalone implements EnvironmentAware {

//ommited...

    @Override
    public void setEnvironment(Environment environment) {
        this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, SPRING_JPA_PROPERTIES);
        new RelaxedPropertyResolver(environment, SPRING_DATASOURCE);
    }

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

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            @Qualifier("dataSource") DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        em.setPackagesToScan(new String[] {"br.com.brb.maf.model"});

        return em;
    }

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

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty(HIBERNATE_HBM2DDL_AUTO,
                jpaPropertyResolver.getProperty(HIBERNATE_HBM2DDL_AUTO, VALIDATE));
        properties.setProperty(HIBERNATE_DIALECT, jpaPropertyResolver.getProperty(HIBERNATE_DIALECT));
        properties.setProperty(HIBERNATE_DEFAULT_SCHEMA, jpaPropertyResolver.getProperty(HIBERNATE_DEFAULT_SCHEMA));
        properties.setProperty(HIBERNATE_SHOW_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_SHOW_SQL, Boolean.FALSE.toString()));
        properties.setProperty(HIBERNATE_FORMAT_SQL,
                jpaPropertyResolver.getProperty(HIBERNATE_FORMAT_SQL, Boolean.FALSE.toString()));
        return properties;
    }
}
@Repository
public class EmprestimoBanknetRepositoryImpl implements EmprestimoBanknetRepositoryCustom {

    private EntityManager entityManager;

    @Autowired
    public EmprestimoBanknetRepositoryImpl(EntityManager manager) {
        this.entityManager = manager;
    }

    @Override
    public LocalDateTime ultimaDataPesquisa() {
        String jpql = "Select distinct max(i.dataOcorrencia) from IndicioEmprestimoBanknet i ";
        Query query = entityManager.createQuery(jpql);
        return (LocalDateTime) query.getSingleResult();
    }

}

错误:

15165 [main] WARN  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emprestimoJobTelebancoScheduler': Unsatisfied dependency expressed through field 'telebancoJob'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'EmprestimoTelebancoJob' defined in class path resource [br/com/brb/maf/application/batch/emprestimo/telebanco/EmprestimoTelebancoConfiguration.class]: Unsatisfied dependency expressed through method 'emprestimoTelebancoJob' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'EmprestimoTelebancoJobStep' defined in class path resource [br/com/brb/maf/application/batch/emprestimo/telebanco/EmprestimoTelebancoConfiguration.class]: Unsatisfied dependency expressed through method 'step' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emprestimoTelebancoReader': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emprestimoServiceImpl': Unsatisfied dependency expressed through field 'telebancoRepository'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emprestimoTelebancoRepositoryImpl' defined in file [C:\Users\u840280\Desktop\SVN\MAF\backend\construcao\branches\DSV_1.0.7\fontes\target\classes\br\com\brb\maf\model\repository\impl\EmprestimoTelebancoRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2: org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1 
15181 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default' 
15181 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default' 
15212 [main] INFO  o.s.b.a.l.AutoConfigurationReportLoggingInitializer - 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 
15228 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in br.com.brb.maf.model.repository.impl.EmprestimoTelebancoRepositoryImpl required a single bean, but 2 were found:
    - org.springframework.orm.jpa.SharedEntityManagerCreator#0: defined by method 'createSharedEntityManager' in null
    - org.springframework.orm.jpa.SharedEntityManagerCreator#1: defined by method 'createSharedEntityManager' in null


Action:

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

2 个答案:

答案 0 :(得分:0)

对我来说,最主要的一点是,当通过EnableJpaRepositories批注定义存储库时,您没有在独立配置中定义事务和实体管理器引用,而是在标准的存储库中进行了定义。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = { "br.com.brb.maf.model.repository" }
)
@Profile("autoContido")
public class DatabaseConfigurationStandalone implements EnvironmentAware

如果您使用的是Hibernate,则管理器可以是同一bean,因此只需将@Profile批注从配置中移至仅DataSource bean。

下面是使用2个数据源的示例:Spring JPA – Multiple Databases 与多个配置文件一起使用,就像向每个配置类添加@Profile注释一样容易,只需确保它们其他注释相同即可。

答案 1 :(得分:0)

我找到了答案。我只需要在EmprestimoBanknetRepositoryImpl中明确设置要使用的EnityManager。因此,最后必须像下面这样:

@Repository
public class EmprestimoBanknetRepositoryImpl implements EmprestimoBanknetRepositoryCustom {

    private EntityManager entityManager;

    @Autowired
    public EmprestimoBanknetRepositoryImpl(@Qualifier("mobileEntityManagerFactory") EntityManager manager) {
        this.entityManager = manager;
    }

    @Override
    public LocalDateTime ultimaDataPesquisa() {
        String jpql = "Select distinct max(i.dataOcorrencia) from IndicioEmprestimoBanknet i ";
        Query query = entityManager.createQuery(jpql);
        return (LocalDateTime) query.getSingleResult();
    }

}