根据提供给工厂bean的属性创建bean

时间:2017-02-01 06:07:43

标签: java spring datasource applicationcontext hikaricp

我必须根据多租户的tenantIdentifier创建一个数据源bean。我正在考虑开箱即用的解决方案,其中添加新租户就像在context.xml中添加配置和应用程序属性文件中的租户属性一样简单,公开API以刷新我的context.xml以从spring cloud配置加载和属性文件。

目前,我遇到了这个错误:

No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: dataSourceFactoryBean,defaultDataSource

问题是,现在我的工厂bean正在创建我想要它创建的bean,但即使是底层bean也在spring-context中注册。这导致了上述问题。

这是我的上下文xml:

<bean id="dataSourceFactoryBean" class="com.comviva.mfs.txn.util.DataSourceFactoryBean" scope="prototype">
    <property name="tenantIdentifier" value="defaultDataSource"/>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="defaultDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig"/>
</bean>

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="txnOracle"/>
    <property name="registerMbeans" value="false"/>
    <property name="dataSourceClassName" value="oracle.jdbc.pool.OracleDataSource"/>
    <property name="maximumPoolSize" value="${mobiquity.database.connection.pool.maximum.pool.size}"/>
    <property name="minimumIdle" value="${mobiquity.database.connection.pool.minimum.pool.size}"/>
    <property name="connectionTimeout" value="${mobiquity.database.connection.pool.connection.timeout}"/>
    <property name="idleTimeout" value="${mobiquity.database.connection.pool.idle.timeout}"/>
    <property name="maxLifetime" value="${mobiquity.database.connection.pool.max.lifetime}"/>
    <property name="dataSourceProperties">
        <props>
            <prop key="url">${mobiquity.database.url}</prop>
            <prop key="user">${mobiquity.database.username}</prop>
            <prop key="password">${mobiquity.database.password}</prop>
            <prop key="implicitCachingEnabled">${mobiquity.database.implicitCachingEnabled}</prop>
            <prop key="maxStatements">${mobiquity.database.maxStatements}</prop>
        </props>
    </property>
    <property name="metricRegistry" ref="platformCommonMetricRegistry"/>
    <property name="healthCheckRegistry" ref="platformCommonHealthCheckRegistry"/>
</bean>

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    <property name="persistenceUnitName" value="NewOracle"/>
    <property name="dataSource" ref="dataSourceFactoryBean"/>
    <property name="jpaDialect" ref="jpaDialect"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="showSql" value="${show.sql}"/>
            <!--<property name="generateDdl" value="true"/>-->
        </bean>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSourceFactoryBean"/>
    <property name="defaultTimeout" value="${default.timeout}"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

我的DataSourceFactoryBean是这样的:

public class DataSourceFactoryBean extends AbstractFactoryBean<DataSource> {

@Autowired
ApplicationContext applicationContext;

private String tenantIdentifier;

public String getTenantIdentifier() {
    return tenantIdentifier;
}

public void setTenantIdentifier(String tenantIdentifier) {
    this.tenantIdentifier = tenantIdentifier;
}

@Override
public Class<DataSource> getObjectType() {
    return DataSource.class;
}

@Override
protected DataSource createInstance() throws Exception {
    DataSource dataSource = (DataSource) applicationContext.getBean(tenantIdentifier);
    return dataSource;
}

有没有办法让我告诉我的entityManagerFactory和transactionManager使用我的工厂bean而不是实际的defaultDataSource bean。

1 个答案:

答案 0 :(得分:1)

尝试将属性<section> <!-- ko foreach : list1 --> <div data-bind='text:$data'></div> <!-- ko foreach : $root.createSubList($data) --> <div data-bind='text:$data'></div> <!-- /ko --> <!-- /ko --> </section> function VM(){ var self = this; self.list1 = [1, 2, 3, 4, 5]; self.createSubList = function(index){ // this method gets called twice, why? console.log(index); return ['Sub1']; } } ko.applyBindings(new VM()) 添加到primary="true" 如果没有这个Spring,有时候无法解析多个bean(即使是限定符)