簡體   English   中英

將Spring xml配置轉換為Java配置

[英]convert spring xml configuration to java configuration

我無法將以下配置轉換為Java配置。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
      <property name="ignoreResourceNotFound" value="true"/>
      <property name="locations">
        <list>
          <value>classpath*:META-INF/spring/environments/${XXX_env}/*.properties</value>
          <value>file:/etc/XXX/database.properties</value>
        </list>
      </property>
    </bean>

在上面的示例中,XXX_env是環境變量。 例如,

export XXX_env=dev

如何將其轉換為Spring Java Configuration? 這是我的嘗試:

@Bean
public PropertyPlaceholderConfigurer propertyConfigurer() throws IOException {
    PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
    props.setIgnoreResourceNotFound(true);
    props.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
    Resource[] locations;
    PathMatchingResourcePatternResolver classPathResources = new PathMatchingResourcePatternResolver();
    locations = classPathResources.getResources("classpath*:META-INF/spring/environments/"+env()+ "/*.properties");
    props.setLocations(locations);
    Resource location = new FileSystemResource("/etc/XXX/database.properties");
    props.setLocation(location);
    return props;
}

private String env(){
    Map<String, String> env = System.getenv();
    return env.get("XXX_env");
}

@Value("${database.password}")
private String databasePassword;

@Value("${database.url}")
private String databaseUrl;

@Value("${database.username}")
private String databaseUsername;

@Value("${database.driverClassName}")
private String databaseDriverClassName;

建議:

Resource location = new FileSystemResource("/etc/XXX/database.properties");

在位置數組中,然后使用setLocations(locations)而不是setLocation(location)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM