我无法将以下配置转换为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;
答案 0 :(得分:0)
建议:
把
Resource location = new FileSystemResource("/etc/XXX/database.properties");
在位置数组中,然后setLocations(locations)
而不是setLocation(location)
。