![](/img/trans.png)
[英]Spring Boot and Spring Data application with multiple DataSources created in runtime
[英]Spring : How to manage datasources created with BeanFactoryPostProcessor, at runtime?
我正在使用Spring Data JPA開發projet。
我設法使用BeanFactoryPostProcessor動態創建數據源,並在使用AbstractRoutingDataSource登錄時切換到所需的數據源。
現在我想在運行時中執行以下操作:
springContext-jpa.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
............
>
<!--
...
...
Spring Data JPA config
...
...
-->
<!-- Parent abstract Datasource -->
<bean id="BasicdsCargestWeb" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- Generic Datasource -->
<bean id="dsCargestWeb" class="com.cargest.custom.CargestRoutingDataSource">
<property name="targetDataSources">
<map>
</map>
</property>
<property name="defaultTargetDataSource" ref="cargestnet1ds" />
</bean>
</beans>
DatasourceRegisteringBeanFactoryPostProcessor.java
@Component
class DatasourceRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// this is my list of Datasources
List<Database> dbs = new ArrayList<Database>();
/*
* Hidden code, here i get my list of Datasources
*/
BeanDefinitionRegistry factory = (BeanDefinitionRegistry) beanFactory;
BeanDefinitionBuilder datasourceDefinitionBuilder;
for (Database db : dbs) {
datasourceDefinitionBuilder = BeanDefinitionBuilder
.childBeanDefinition("BasicdsCargestWeb")
.addPropertyValue("url", db.getUrl()+db.getName()+"?autoReconnect=true");
factory.registerBeanDefinition("cargestnet"+db.getId()+"ds",
datasourceDefinitionBuilder.getBeanDefinition());
}
// Configure the dataSource bean properties
MutablePropertyValues mpv = factory.getBeanDefinition("dsCargestWeb").getPropertyValues();
// Here you can set the default dataSource
mpv.removePropertyValue("defaultTargetDataSource");
mpv.addPropertyValue("defaultTargetDataSource",
new RuntimeBeanReference("cargestnet1ds"));
// Set the targetDataSource properties map with the list of connections
ManagedMap<String, RuntimeBeanReference> mm = (ManagedMap<String, RuntimeBeanReference>) mpv.getPropertyValue("targetDataSources").getValue();
System.out.println("list size "+mm.size());
mm.clear();
for (Database db : dbs) {
mm.put(db.getId().toString(), new RuntimeBeanReference("cargestnet"+db.getId()+"ds"));
}
}
}
問題在於BeanFactoryPostProcessor類使用ConfigurableListableBeanFactory作為beanFactory。
我需要從另一個類(在運行時)中訪問相同的beanFactory,以修改我的數據源映射(dsCargestWeb-> targetDataSources->映射)。
謝謝
我認為您可以嘗試以下架構:
DataSourceS
的初始映射 AbstractRoutingDataSource
實現保存了DataSourceS
的映射,並最初注入了上述工廠bean 這樣,如果以后需要添加一個新的DataSource
,你可以得到從地圖AbstractRoutingDataSource
實施,並直接將其添加到它,或者把一個方法在AbstractRoutingDataSource
實現添加新的DataSource
。
作為上述方法的一個變體,您可以創建一個類似的AbstractRoutingDataSource
實現,並向其注入另一個bean,該bean的init-method
中將計算DataSourceS
映射並將其散布在路由數據源中。
使用RuntimeBeanReference
和ManagedSet
,可以將運行時設置設置為beanDefine
。 這是使用這些的正確方法:
ManagedSet<RuntimeBeanReference> processorSet = new ManagedSet<RuntimeBeanReference>();
for (String processorBeanName : processorBeanNames) {
processorSet.add(new RuntimeBeanReference(processorBeanName));
}
beanDefine.getPropertyValues().addPropertyValue(new PropertyValue(key, value));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.