在Spring Java配置中引用$ {user.home}

时间:2013-02-14 14:38:51

标签: java spring configuration

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

1 个答案:

答案 0 :(得分:13)

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