当前位置: 首页 > 知识库问答 >
问题:

根据赋予工厂bean的属性创建bean

方奕
2023-03-14

我必须根据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 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个答案

白腾
2023-03-14

尝试将属性primary=“true”添加到 中,如果没有这个Spring,有时无法解析多个bean(即使使用限定符)

 类似资料:
  • 我有一个factory-ish bean,它在启动时创建了许多对象,我希望这些对象本身就是Spring bean。 如果我创建一个对象,我可以使用工厂方法进行实例化,例如(来自Spring文档第4.3.2.3节): 如果我提前知道我有n个对象,我可以调用n个不同的方法,但是我没有——我的工厂创建了任意数量的对象,而这些对象是提前不知道的。 有人知道怎么做吗? 目标是让它们成为“合适的”Spring

  • 我有一些Spring托管类(通过xml配置),其中一个是SessionFactory,它被注入到另一个Spring托管类中。每当该类需要一个新的会话时,它就调用SessionFactory上的createSession。

  • 我需要执行从Micronaut到Spring应用程序的远程调用。为了创建必要的bean,我创建了一个工厂: 在我的Spock集成测试中,我需要模拟这些bean,我根据Micronaut文档进行了尝试:https://docs.micronaut.io/latest/guide/index.html#replaces 这导致了这样的测试: 这个解决方案效果不佳。如果这两个测试是单独运行的,那么这两个

  • 问题内容: 我有以下代码: factory route controller 为什么console.log($ scope.item.url); 给我不确定的? 但在视图中我拥有所有数据 html ENDS UP 就像@Frank van Puffelen的评论一样,我的解决方法是: 问题答案: 这是因为在您运行时,尚未从Firebase加载数据。Angular侦听Firebase / Angul

  • 我继承了一个旧的(15年)Java应用程序,它维护得很糟糕。Mavenizing没有正确地完成,所以我尝试了一下,因为很多库都是手工添加到类路径中的。现在我有了一个主pom,在pom文件中有所有的库作为依赖项。 错误如下: 我已经升级到Java8(也在Liberty Profile上运行,因为我们使用IBM JDK 8绑定到IBM)。添加了jaxp-api以获得类路径上的'Access Exter

  • 实际应用程序注册一个来添加bean定义。实例本身是通过starter项目中定义的另一个bean构造的,而starter项目本身将另一个bean作为依赖项。 为了使用动态注册的bean,我创建了一个用注释的类,并定义了一个将所述bean作为参数的构造函数。当我通过设置来调试应用程序时,我可以看到在创建动态bean之前调用了组件的构造函数。而且,当时连工厂豆都还没有创建出来。 将带有工厂bean名称的