Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-10325

Allow for ignoreResourceNotFound on @PropertySource

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.2.1
    • Fix Version/s: 4.0 GA
    • 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.

        Issue Links

          Activity

          Hide
          robin 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 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:
              juergen.hoeller Juergen Hoeller
              Reporter:
              wesleyhall Wesley Hall
              Last updater:
              Juergen Hoeller
            • Votes:
              11 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                2 years, 4 weeks, 3 days ago