[SPR-10325] Allow for ignoreResourceNotFound on @PropertySource Created: 22/Feb/13  Updated: 09/Aug/14  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:
Duplicate
duplicates SPR-8371 Add @PropertySources annotation and s... Closed
Days since last comment: 5 years, 17 weeks, 3 days ago
Last commented by a User: true
Last updater: Juergen Hoeller

 Description   

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.



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

@Configuration
@PropertySource("classpath:default.properties")
public class BaseConfig {
  @Inject
  private ApplicationContext context;

  @PostConstruct
  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?

Generated at Sun Aug 19 17:31:57 UTC 2018 using JIRA 7.9.2#79002-sha1:3bb15b68ecd99a30eb364c4c1a393359bcad6278.