簡體   English   中英

根據賦予工廠bean的屬性創建一個bean

[英]Create a bean depending on the property given to the factory bean

我必須根據tenantIdentifier創建一個多租戶數據源bean。 我正在考慮一種開箱即用的解決方案,其中添加新租戶就像在context.xml中添加配置以及在應用程序屬性文件中添加租戶屬性一樣簡單,公開一個API刷新我的context.xml以從Spring Cloud config中加載和屬性文件。

目前,我陷入了這個錯誤:

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。

嘗試將屬性primary="true"添加到<bean id="defaultDataSource"否則,Spring有時無法解析多個bean(即使使用限定符)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM