The project has already implemented multi-data source configuration in beans. directly configure multiple data source beans in the xml file, and then dynamically switch the data source through HotSwappableTargetSource when using the data source (Google for details ). If the owner is not satisfied, only the corresponding connection information must be configured in the attribute file, and the dynamic data source must inherit the default data source attribute except the configured attribute. The data source format in the property file is as follows:
[Plain]
# Connect to the database address, which can be dynamically added, "link. ",". the name before jdbc is the name of the database connection pool. If other connection pool attributes are not written, the Hummer database connection pool configuration will be automatically inherited.
Link. eagle2.business. jdbc. jdbcUrl = jdbc: oracle: thin: @ 192.168.0.155: 1521: orcl
Link. eagle2.business. jdbc. user = eagle2
Link. eagle2.business. jdbc. password = eagle2_password
Link. eagle2.interface. jdbc. jdbcUrl = jdbc: oracle: thin: @ 192.168.0.155: 1521: interface
Link. eagle2.interface. jdbc. user = interface
Link. eagle2.interface. jdbc. password = interface22
Link. eagle2.ods. jdbc. jdbcUrl = jdbc: oracle: thin: @ 192.168.0.10: 1521: sifen
Link. eagle2.ods. jdbc. user = honghe
Link. eagle2.ods. jdbc. password = honghe_pwd
In order to achieve this requirement, I searched by google and tried to write code. Finally, the function was well implemented, and the results were recorded as follows:
Implement a dynamic‑cec3p0 class that implements the ApplicationContextAware and ApplicationListener interfaces. Implement ApplicationContextAware to get ApplicationContext, and implement ApplicationListener to configure spring loading events.
The implementation code of the class is as follows:
[Java]
Import java. io. IOException;
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Map;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Import org. springframework. beans. BeansException;
Import org. springframework. beans. factory. config. BeanDefinition;
Import org. springframework. beans. factory. support. ChildBeanDefinition;
Import org. springframework. beans. factory. support. DefaultListableBeanFactory;
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. ApplicationContextAware;
Import org. springframework. context. ApplicationEvent;
Import org. springframework. context. ApplicationListener;
Import org. springframework. context. event. ContextRefreshedEvent;
Import org. springframework. core. io. support. ResourcePropertySource;
Import com. mchange. v2.c3p0. ComboPooledDataSource;
Public class DynamicDataSourceC3p0 implements ApplicationContextAware, ApplicationListener {
Private ApplicationContext app;
@ Override
Public void setApplicationContext (ApplicationContext app)
Throws BeansException {
This. app = app;
}
Public void onApplicationEvent (ApplicationEvent event ){
// For a container refresh event
If (event instanceof ContextRefreshedEvent) {// if it is a container Close event
Try {
RegDynamicBean ();
} Catch (IOException e ){
E. printStackTrace ();
}
// System. out. println (event. getClass (). getSimpleName () + "the event has occurred! ");
}
}
Private void regDynamicBean () throws IOException {
// Parse the attribute file to obtain the data source Map
Map <String, performanceinfo> mapCustom = parsePropertiesFile ("hummer. properties ");
// Register the data source bean to the container
AddSourceBeanToApp (mapCustom );
}
/**
* Function Description: Creates a bean Based on DataSource and registers it to the container.
* @ Param acf
* @ Param mapCustom
*/
Private void addSourceBeanToApp (Map <String, DataSourceInfo> mapCustom ){
DefaultListableBeanFactory acf = (DefaultListableBeanFactory) app. getAutowireCapableBeanFactory ();
BeanDefinition beanDefinition;
Iterator <String> iter = mapCustom. keySet (). iterator ();
While (iter. hasNext ()){
String beanKey = iter. next ();
// Get the Bean definition and add it to the container
BeanDefinition = new ChildBeanDefinition ("dataSource ");
// Note: the Bean must be registered in the container before modification. Otherwise, the data source attribute cannot be modified effectively.
Acf. registerBeanDefinition (beanKey, beanDefinition );
// Obtain the Bean definition of the data source and modify the connection-related attributes.
ComboPooledDataSource cpd= (ComboPooledDataSource) app. getBean (beanKey );;
CPPS. setJdbcUrl (mapCustom. get (beanKey). connUrl );
CPPS. setUser (mapCustom. get (beanKey). userName );
CPPS. setPassword (mapCustom. get (beanKey). password );
}
}
/**
* Function Description: parse the attribute file to obtain the data source Map.
* @ Return
* @ Throws IOException
*/
Private Map <String, performanceinfo> parsePropertiesFile (String fileName) throws IOException {
// Attribute File
ResourcePropertySource props = new ResourcePropertySource (fileName );
Matcher;
Pattern pattern = Pattern. compile ("^ link \\. (eagle2 \\. \ w + )\\. jdbc \\. (jdbcUrl | user | password) $ ");
Map <String, DataSourceInfo> mapDataSource = new HashMap <String, DataSourceInfo> ();
// Parse the data source according to the configuration file
For (String keyProp: props. getPropertyNames ())
{
Matcher = pattern. matcher (keyProp );
If (matcher. find ()){
String dsName = matcher. group (1 );
String dsPropName = matcher. group (2 );
Performanceinfo dsi;
If (mapDataSource. containsKey (dsName )){
Dsi = mapDataSource. get (dsName );
}
Else {
Dsi = new performanceinfo ();
}
// Assign a value to the data source Attribute Based on the attribute name
If ("jdbcUrl". equals (dsPropName )){
Dsi. connUrl = (String) props. getProperty (keyProp );
} Else if ("user". equals (dsPropName )){
Dsi. userName = (String) props. getProperty (keyProp );
} Else if ("password". equals (dsPropName )){
Dsi. password = (String) props. getProperty (keyProp );
}
MapDataSource. put (dsName, dsi );
}
}
Return mapDataSource;
}
Private class performanceinfo {
Public String connUrl;
Public String userName;
Public String password;
Public String toString (){
Return "(JcbcUrl:" + connUrl + ", user:" + userName + ", password:" + password + ")";
}
}
}
Add the configuration in the beans. xml file.
"ApplicationEventListener" class = "com. dfsoft. hummer. domain. common. dynamic1_cec3p0"/>
Let spring call the onApplicationEvent method in dynamic1_cec3p0 during loading to dynamically register the data source. The original dynamic switching of multiple data sources does not need to be modified.
Additional instructions:
1. After completing the above work, remove the original multi-data source configuration in beans. xml (no longer used ).
2. acf. registerBeanDefinition does not need to judge whether the bean with the corresponding name already exists in the container. spring will overwrite the bean with this name and add it if it does not exist.