[SPR-10325] Allow for ignoreResourceNotFound on @PropertySource Created: 22/Feb/13  Updated: 15/Jan/19  Resolved: 09/Aug/14

Status: Resolved
Project: Spring Framework
Component/s: Core:Environment
Affects Version/s: 3.2.1
Fix Version/s: 4.0 GA

Type: Improvement Priority: Minor
Reporter: Wesley Hall Assignee: Juergen Hoeller
Resolution: Complete Votes: 11
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
duplicates SPR-8371 Add @PropertySources annotation and s... Closed
Days since last comment: 4 weeks, 6 days ago
Last commented by a User: true
Last updater: Spring Issuemaster


It doesn't appear to be possible to set up @PropertySource annotations that refer to non-existent files.

It can be quite useful to allow for optional configuration files, for example, ~/.application/application.properties to allow for the optional overriding of configuration properties on a per user basis. I might not have the file, but if I do, it's values should override the defaults.

It is possible to achieve this behaviour with 'setIgnoreResourceNotFound' on the PropertyPlaceholderConfigurer, but the new @PropertySource annotation will throw a FileNotFoudException and the application context fails to start.

Consider adding an attribute to @PropertySource to allow for this behaviour, and potentially a @PropertySources annotation which can be configured with an array of @PropertySource values to allow for file configuration on a per file basis.

Comment by Robin Sander [ 25/Apr/13 ]

It's too bad that such a basic usecase isn't supported by Spring out of the box! (I'm using Spring 3.1)
As a workaround I'm using a MutablePropertySources in a @PostConstruct method like this:

public class BaseConfig {
  private ApplicationContext context;

  public void init() throws IOException {
    Resource optionalProps = context.getResource("classpath:optional.properties");
    if (optionalProps.exists()) {
      MutablePropertySources sources = ((ConfigurableApplicationContext) context).getEnvironment().getPropertySources();
      sources.addFirst(new ResourcePropertySource(optionalProps));

But then I have to ensure that this code gets executed before any other @Configuration instance and until now, I can't find any hint about how to achieve this. At least the order of @Configuration instances seems not to be determined by the @Import annotations (see SPR-10409) and that's why I'm currently injecting BaseConfig in any other @Configuration class to force Spring to initialize this configuration first.
Or am I missing the obvious here and @Order is supported for @Configuration classes, too?

Comment by Spring Issuemaster [ 14/Jan/19 ]

The Spring Framework has migrated to GitHub Issues. This issue corresponds to spring-projects/spring-framework#14959.

Generated at Mon Feb 18 12:02:18 UTC 2019 using JIRA 7.9.2#79002-sha1:3bb15b68ecd99a30eb364c4c1a393359bcad6278.