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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14877230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:43:21  来源:igfitidea点击:

reference ${user.home} in Spring Java configuration

javaspringconfiguration

提问by shmish111

In xml configuration I can do the following:

在 xml 配置中,我可以执行以下操作:

<context:property-placeholder
        location="file:${user.home}/.config}/api.properties"
        ignore-resource-not-found="true"
        system-properties-mode="OVERRIDE"/>

In java configuration I would do the following:

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

However this doesn't understand ${user.home}

然而这不理解 ${user.home}

回答by AlexR

Try ${sys:user.home}to refer system property. For system environment use ${env:THE-ENV-VAR-NAME}

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