如何在spring-boot应用程序中从YML文件加载多个属性

时间:2016-03-30 05:11:29

标签: java spring configuration spring-boot

我们有一个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 
}

我的弹簧启动配置文件如下:

@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);
  }
}

结果与预期不符。

  1. 当我从@Value类中删除DataSources时,属性readDataSourcewriteDataSource都为空(DataSources类本身不为空)。< / LI>
  2. 当我在@Value课程中添加DataSources时,测试失败,异常

    引起:java.lang.IllegalStateException:无法将[java.lang.String]类型的值转换为必需类型[com.abc.DataSource]:找不到匹配的编辑器或转换策略

  3. 感谢有人能提供一些如何处理这个问题的想法。我想要的只是在类似readDataSource的类中捕获writeDataSourceDataSources

1 个答案:

答案 0 :(得分:6)

使用DataSources为您的@Configuration课程添加注释,然后创建使用@Bean注释的2 @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 Reference Guide中的here解释了这种机制。

如果您不需要DataSource但是构建自己的对象(虽然不确定为什么需要它?),这同样适用。