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

shm*_*111 8 java configuration spring

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

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

在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;
}
Run Code Online (Sandbox Code Playgroud)

但是这不理解$ {user.home}

Ale*_*exR 13

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

  • 不起作用<context:property-placeholder system-properties-mode ="ENVIRONMENT"order =" - 1"location ="file:$ {env:user.home} /managed/latest/bin/rm-app.properties "/> (2认同)