例如要注入下列参数:
dyn.spring.datasources[0].name=branchta
dyn.spring.datasources[0].driverClassName=oracle.jdbc.OracleDriver
dyn.spring.datasources[0].url=jdbc:oracle:thin:@IP:1521:ora11g
dyn.spring.datasources[0].username=scott
dyn.spring.datasources[0].password=tiger
dyn.spring.datasources[1].name=ta4
dyn.spring.datasources[1].driverClassName=oracle.jdbc.OracleDriver
dyn.spring.datasources[1].url=jdbc:oracle:thin:@IP:1521:ora11g
dyn.spring.datasources[1].username=hr
dyn.spring.datasources[1].password=hr
这样,通过结合RootBeanDefinition以及DefaultListableBeanFactory相关接口,就可以纯编程式动态创建bean,而无需修改xml或者配置bean,达到纯动态的目的。
但是网上的例子似乎都是注入string类型的list,经测试,只要将datasources包含的类型定义为public即可,如下:
package com.XX.XXX.datasource; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; @ConfigurationProperties(prefix="dyn.spring") @PropertySource("classpath:jrescloud.properties") public class DynamicDataSourceConfig { private List<DataSource> datasources; public static class DataSource { private String name; private String driverClassName; private String url; private String username; private String password; private String maxActive; private String maxIdle; private String maxWait; private String validationQuery; private String defaultAutoCommit; private String connectionInitSqls; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getMaxActive() { return maxActive; } public void setMaxActive(String maxActive) { this.maxActive = maxActive; } public String getMaxIdle() { return maxIdle; } public void setMaxIdle(String maxIdle) { this.maxIdle = maxIdle; } public String getMaxWait() { return maxWait; } public void setMaxWait(String maxWait) { this.maxWait = maxWait; } public String getValidationQuery() { return validationQuery; } public void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public String getDefaultAutoCommit() { return defaultAutoCommit; } public void setDefaultAutoCommit(String defaultAutoCommit) { this.defaultAutoCommit = defaultAutoCommit; } public String getConnectionInitSqls() { return connectionInitSqls; } public void setConnectionInitSqls(String connectionInitSqls) { this.connectionInitSqls = connectionInitSqls; } } public List<DataSource> getDatasources() { return datasources; } public void setDatasources(List<DataSource> datasources) { this.datasources = datasources; } }
这样就可以注入了。。。。。:)