根据给工厂 bean 的 属性 创建一个 bean
Create a bean depending on the property given to the factory bean
我必须根据多租户的 tenantIdentifier 创建数据源 bean。我正在考虑开箱即用的解决方案,其中添加新租户就像在 context.xml 中添加配置和在应用程序属性文件中添加租户属性一样简单,公开一个 API 来刷新我的 [=26] =] 从 spring 云配置和属性文件加载。
目前,我遇到了这个错误:
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(即使有限定符)
我必须根据多租户的 tenantIdentifier 创建数据源 bean。我正在考虑开箱即用的解决方案,其中添加新租户就像在 context.xml 中添加配置和在应用程序属性文件中添加租户属性一样简单,公开一个 API 来刷新我的 [=26] =] 从 spring 云配置和属性文件加载。
目前,我遇到了这个错误:
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(即使有限定符)