Spring的@Qualifier无法正常运行

时间:2018-08-28 08:39:49

标签: java spring

有两种数据源配置类:

@Configuration
@EnableJpaRepositories(
        basePackages = {"com.proj.killbill.repository.kb", "com.proj.service.base.repository"},
        entityManagerFactoryRef = "kbEntityManagerFactory",
        transactionManagerRef = "kbTransactionManager")
@EnableJpaAuditing
@Profile({"dev", "prod"})
public class PersistenceKbConfig {

    @Value("${db.user:default}")
    private String user;

    @Value("${db.password:default}")
    private String password;

    @Value("${db.url:default}")
    private String jdbcUrl;

    @Value("${app.kb.ds.jndi.name}")
    private String kbDsJndiName;

    @Bean(name = "kbDataSource")
    @Profile("dev")
    public DataSource devKbDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "kbDataSource")
    @Profile("prod")
    public DataSource prodKbDataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        return dsLookup.getDataSource(kbDsJndiName);
    }

    @SuppressWarnings("Duplicates")
    @Bean(name = "kbEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            @Qualifier("kbDataSource") DataSource dataSource) {

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan("com.proj.service.base.domain");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        return em;
    }

    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
        return properties;
    }

    @Bean(name = "kbTransactionManager")
    public PlatformTransactionManager kbTransactionManager(
            @Qualifier("kbEntityManagerFactory") EntityManagerFactory emf) {

        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}


@Configuration
@EnableJpaRepositories(
        basePackages = {"com.proj.killbill.repository.cm", "com.proj.service.base.repository"},
        entityManagerFactoryRef = "cmEntityManagerFactory",
        transactionManagerRef = "cmTransactionManager")
@EnableJpaAuditing
@Profile({"dev", "prod"})
public class PersistenceCmConfig {

    @Value("${cm.db.user:default}")
    private String user;

    @Value("${cm.db.password:default}")
    private String password;

    @Value("${cm.db.url:default}")
    private String jdbcUrl;

    @Value("${app.cm.ds.jdni.name}")
    private String cmJndiName;

    private static final Logger LOGGER = LogManager.getLogger(PersistenceCmConfig.class);

    @Bean(name = "cmDataSource")
    @Profile("dev")
    public DataSource devCmDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        LOGGER.debug("CM JDBC URL USER/PASSWORD" + jdbcUrl + " " + user + "/" + password);
        return dataSource;
    }

    @Bean(name = "cmDataSource")
    @Profile("prod")
    public DataSource prodCmDataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        return dsLookup.getDataSource(cmJndiName);
    }

    @SuppressWarnings("Duplicates")
    @Bean(name = "cmEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean cmEntityManagerFactory(
            @Qualifier("cmDataSource") DataSource dataSource) {

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan("com.proj.service.base.domain");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        return em;
    }

    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
        return properties;
    }

    @Bean(name = "cmTransactionManager")
    public PlatformTransactionManager cmTransactionManager(
            @Qualifier("cmEntityManagerFactory") EntityManagerFactory emf) {

        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}

实体管理器使用@Qualifier批注注入适当的数据源。但是,当我启动该应用程序时,出现了异常:

==========

com.proj.killbill.config.PersistenceCmConfig中方法cmEntityManagerFactory的参数0需要单个bean,但是找到了2个:     -cmDataSource:由类路径资源[com / proj / killbill / config / PersistenceCmConfig.class]中的方法'devCmDataSource'定义     -kbDataSource:由类路径资源[com / proj / killbill / config / PersistenceKbConfig.class]中的方法'devKbDataSource'定义

操作:

考虑将其中一个bean标记为@Primary,更新使用者以接受多个bean,或使用@Qualifier标识应消耗的bean

============

@Qualifier(“ cmDataSource”)由于未知原因而被忽略。为什么有可能?

0 个答案:

没有答案