在 xml 配置中,我可以执行以下操作:
<context:property-placeholder
location="file:${user.home}/.config}/api.properties"
ignore-resource-not-found="true"
system-properties-mode="OVERRIDE"/>
在 java 配置中,我会执行以下操作:
/**
* @return a {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer} so that placeholders are correctly populated
* @throws Exception exception if the file is not found or cannot be opened or read
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws Exception {
PropertySourcesPlaceholderConfigurer propConfig = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new UrlResource[]
{new UrlResource("file:${user.home}/.config/api.properties")};
propConfig.setLocations(resources);
propConfig.setIgnoreResourceNotFound(true);
propConfig.setIgnoreUnresolvablePlaceholders(true);
return propConfig;
}
但是这不明白 ${user.home}