Spring Framework
  1. Spring Framework
  2. SPR-10325

Allow for ignoreResourceNotFound on @PropertySource

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 3.2.1
    • Fix Version/s: Waiting for Triage
    • Component/s: Core:Environment
    • Labels:
      None
    • Last commented by a User:
      true

      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.

        Activity

        Hide
        Robin Sander added a comment -

        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?

        Show
        Robin Sander added a comment - 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?

          People

          • Assignee:
            Chris Beams
            Reporter:
            Wesley Hall
            Last updater:
            Robin Sander
          • Votes:
            10 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since last comment:
              51 weeks, 3 days ago