9

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}

shmish111
  • 3,697
  • 5
  • 30
  • 52
  • Someone has upvoted this today so I don't see how the fact that it is a duplicate matters, clearly this currently has the correct answer to solve the problem. Also I can't find a duplicate, can the moderators please provide a link? – shmish111 Jun 27 '14 at 11:45
  • This is NOT a duplicate of "Change user.home system property", as mentioned above! It is related, but not the same! Please remove the duplicate banner, or provide a proper link to a similar question. – deluan Dec 09 '15 at 15:03
  • This is Not a duplicate, it is specifically referring to use of Properties in Spring which has its own syntax and configuration attributes see http://www.baeldung.com/2012/02/06/properties-with-spring/ – Nigel Savage Sep 26 '16 at 19:30
  • This is not a duplicate – Neil Benn Sep 05 '17 at 10:14

1 Answers1

15

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

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 2
  • @Arun BC, if it does not work *for you* this is not a reason for downvoting. The reason in your case is that you want to use system variable `user.home` and therefore should use syntax `${sys:user.home}`. You however turn to environment variable that obviously does not exist. – AlexR Sep 30 '13 at 15:44