java - Spring : How to manage datasources created with BeanFactoryPostProcessor, at runtime? -
I am working on a project using Spring Data JPA.
I have been able to create dynamic databasers with BeanFactoryPostProcessor and switch to the desired one when I log in using Structuring Data . Now, let me have a map of dynamic data sources with springContext-jpa.xml The problem is that BeanFactoryPostProcessor is using ConfigurableListableBeanFactory as class beanFactory. To modify the map of I (DsCargestWeb -> targetDataSources -> Maps), the second invoice (in runtime) needs to reach the same beef factor. Thanks I think you can try the following architecture: In this way, if you later To add new As an above version, you can create an identical
& Lt ;? Xml version = "1.0" encoding = "UTF-8" & gt; & Lt; Bem 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: reference =" 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" ............ & gt; ; & Lt ;! - ... ... spring data jpa config ... ... - & gt; & Lt ;! - Original abstract data source - & gt; & Lt; Bean id = "BasicdsCargestWeb" class = "org.apache.commons.dbcp.BasicDataSource" & gt; & Lt; Property Name = "driverClassName" value = "$ {jdbc.driverClassName}" & gt; & Lt; / Property & gt; & Lt; Property Name = "User Name" Value = "$ {jdbc.username}" & gt; & Lt; / Property & gt; & Lt; Property Name = "Password" Value = "$ {jdbc.password}" & gt; & Lt; / Property & gt; & Lt; / Bean & gt; & Lt ;! - Generic Datasource - & gt; & Lt; Bean id = "dsCargestWeb" class = "com.cargest.custom.CargestRoutingDataSource" & gt; & Lt; Property Name = "targetDataSources" & gt; & Lt; Map & gt; & Lt; / Map & gt; & Lt; / Property & gt; & Lt; Property Name = "defaultTargetDataSource" ref = "cargestnet1ds" / & gt; & Lt; / Bean & gt; & Lt; / Bean & gt; My list of database list & lt; Database & gt; DBS = new arreelist & lt; Database & gt; (); / * * Hidden code, Here I get the list of my data sources / * BinDefinitionregistry factory = (BeanDefinitionRegister) Beenfactor; BinDefinitionBuilder DatastetDefinitionBuilder; (Database DB: DBS) {Dataset DefinitionBuilder = BinDefinitionBuilder Childbank Definition ("BasicGeserveWeb") .addPropertyValue ("url", db.getUrl () + db.getName () + "? Auto-reconnect = true"); Factory.registerBeanDefinition ("cargestnet" + db.getId () + "DS", Dataset Definition Builder .getBeanDefinition ()); } // Configure data source bean properties. MutablePropertyValues mpv = factory.getBeanDefinition ("dsCargestWeb") GetPropertyValues (); // Here you can set the default data source mpv.removePropertyValue ("defaultTargetDataSource"); Mpv.addPropertyValue ("defaultTargetDataSource", new runtime binreference ("cargestnet1ds")); // Set the list of connections with list of targetDataSource properties ManagedMap & lt; String, RuntimeBinReference & gt; Mm = (ManagedMap & lt; String, RuntimeBeanReference & gt;) mpv.getPropertyValue ("targetDataSources"). GetValue (); System.out.println ("list size" + mm.size ()); Mm.clear (); For (Database DB: DBS) {mm} (DB.GetID.) Tosting (), New Runtimebine Reference ("Corgeestate" + DB.GetID (+ + "DS")); }}}
DataSourceS
DataSourceS , and after that initially The version of the factory is injected with beans
DataSourceS
AbstractRoutingDataSource
datasource , you can either type
AbstractRoutingDataSource Get the map from the implementation and add it directly, or put a method to add a new
AbstractRoutingDataSource implementation, and with it one more bean, its
Init-method Calculates the
DataSourceS map and extends it into the routing data source.
Comments
Post a Comment