[英]How to load multiple properties from YML file in spring-boot application
We have a YML configuration which looks like: 我们有一个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
We have to load this to a custom class DataSources
which looks like 我们必须将其加载到自定义类
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
}
My configuration files for spring boot looks like: 我的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);
}
}
The unit test is: 单元测试是:
@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);
}
}
Result is not as expected. 结果不符合预期。
@Value
from DataSources
class, both the properties readDataSource
and writeDataSource
are null (The DataSources
class itself is not null). DataSources
类中删除@Value
,属性readDataSource
和writeDataSource
都为null( DataSources
类本身不为null)。 When I add @Value
in DataSources
class, the test fails with exception 当我在
DataSources
类中添加@Value
时,测试失败并出现异常
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.abc.DataSource]: no matching editors or conversion strategy found 原因:java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为所需的类型[com.abc.DataSource]:未找到匹配的编辑器或转换策略
Appreciate if someone can provide some idea how to deal with this. 欣赏是否有人可以提供一些解决方法。 All I want is to capture both
readDataSource
and writeDataSource
inside a class like DataSources
. 我想要的是在类似
DataSources
的类中捕获readDataSource
和writeDataSource
。
Annotate your DataSources
class with @Configuration
then create 2 @Bean
methods annotatated with @ConfigurationProperties
. 注释你的
DataSources
类@Configuration
再创建2种@Bean
与annotatated方法@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();
}
}
Now you have 2 datasources with the properties bound to the created DataSource
s. 现在,您有2个数据源,其属性绑定到创建的
DataSource
。 This mechanism is explained here in the Spring Boot Reference Guide. 这种机制被解释这里的春天引导参考指南中。
The same would apply if you don't need a DataSource
but construct your own object (although not sure why you would need that?). 如果您不需要
DataSource
而是构造自己的对象(尽管不确定为什么会需要它),则同样适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.