我们有一个YML配置,如下所示:
datasurces:
readDataSource:
ssl-enabled: false
driver-class-name: oracle.jdbc.driver.OracleDriver
host: db1.abc.com
port: 1232
sid: ABC_DB
trust-store-fileName: abcdb.jks
connection-pool:
initial-size: 10
max-size: 20
writeDataSource:
ssl-enabled: false
driver-class-name: oracle.jdbc.driver.OracleDriver
host: db2.abc.com
port: 1232
sid: XYZ_DB
trust-store-fileName: xyzdb.jks
connection-pool:
initial-size: 10
max-size: 20
我们必须将其加载到自定义类
DataSources
@ConfigurationProperties(prefix = "datasources")
public class DataSources {
@Value("${datasources.readDataSource}")
private DataSource readDataSource;
@Value("${datasources.writeDataSource}")
private DataSource writeDataSource;
//...getters/setters
}
public class DataSource {
private String id;
private boolean sslEnabled;
private String driverClassName;
private String host;
private int port;
private String trustStoreFileName;
private ConnectionPool connectionPool;
//...getters/setters
}
public class ConnectionPool {
private int initialSize;
private int maxSize;
//...getters/setters
}
我的Spring Boot的配置文件如下所示:
@Configuration
@ComponentScan(basePackages = {"com.abc"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableConfigurationProperties(DataSources.class)
@Profile({"dev"})
public class TestAppConfiguration {
}
@EnableAutoConfiguration
@SpringBootApplication
@EnableConfigurationProperties(DataSources.class)
public class TestAppInitializer {
@Autowired
private DataSources dataSources;
public static void main(final String[] args) {
SpringApplication.run(TestAppInitializer.class, args);
}
}
单元测试是:
@SpringApplicationConfiguration(classes = {TestAppInitializer.class})
@Test(groups = "categoryTests")
@ActiveProfiles("dev")
public class DataSourcesTest extends AbstractTestNGSpringContextTests {
private static final AppLogger logger = LoggerUtil.getLogger(DataSourcesTest.class);
@Autowired
private DataSources dataSources;
@Test
public void printDetails() {
logger.debug("DataSources --> {}", dataSources);
}
}
结果与预期不符。
当我从
@Value
类中删除DataSources
时,属性readDataSource
和writeDataSource
都为空(DataSources
类本身不为空)。当我在
@Value
类中添加DataSources
时,测试失败并出现异常原因:java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为必需的类型[com.abc.DataSource]:未找到匹配的编辑器或转换策略
欣赏是否有人可以提供一些解决方法。我只想在
readDataSource
之类的类中同时捕获writeDataSource
和DataSources
。 最佳答案
用DataSources
注释您的@Configuration
类,然后创建2个用@Bean
注释的@ConfigurationProperties
方法。
@Configuration
public class DataSources {
@Bean
@ConfigurationProperties(prefix="datasources.readDataSource")
public DataSource readDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="datasources.writeDataSource")
public DataSource writeDataSource() {
return DataSourceBuilder.create().build();
}
}
现在,您有2个数据源,其属性绑定到创建的
DataSource
。 《 Spring Boot参考指南》中的here中对此机制进行了说明。如果您不需要
DataSource
而是构造自己的对象(尽管不确定为什么会需要它),则同样适用。