Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference ${user.home} in Spring Java configuration [duplicate]

In xml configuration I can do the following:

<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:

/**
 * @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}

like image 303
shmish111 Avatar asked Feb 14 '13 14:02

shmish111


1 Answers

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

like image 196
AlexR Avatar answered Sep 20 '22 06:09

AlexR