8

在 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}

4

1 回答 1

14

尝试${sys:user.home}引用系统属性。供系统环境使用${env:THE-ENV-VAR-NAME}

于 2013-02-14T14:48:25.097 回答