Jens D
Jens D

Reputation: 4676

How can Spring only select one resource (properties file) when several may exist on the classpath

When configuring Spring security, I'm using the basic in-memory authentication manager which reads credentials from a properties file (creds.properties). For example:

<authentication-manager>
  <authentication-provider>
    <user-service properties="classpath:creds.properties">
    </user-service>
  </authentication-provider>
</authentication-manager>

My app is delivered with a default file containing a default user, but I would like to have the user provide their own file on the classpath. So, I could specify the properties file with a wildcard, like classpath*:creds.properties. However, the caveat is that I only want one of the property files to be used (the first one found) and that wildcard is going to pick up all matching files and merge the results. I would only want the creds, provided by the user override, to be in effect.

The Spring docs seem to imply that I could also wildcard the path, Ant-style as classpath:/**/creds.properties, but this didn't work for me as none of the files were even found.

How could I achieve this in the above scenario?

I started looking at the PropertiesFactoryBean, but it doesn't seem to support my need directly. My use-case might be solved by extending PropertiesFactoryBean (which itself extends PropertiesLoaderSupport) and overriding loadProperties but that also isn't going to work very easily as that class doesn't really seem to be designed to be extended without, basically, copying the whole thing.

Upvotes: 0

Views: 112

Answers (1)

Ricardo Veguilla
Ricardo Veguilla

Reputation: 3155

My recommendation : don't use the classpath for configuration discovery.

I prefer to check for configuration in following order:

  1. Check a system property for the location of the configuration file.

    For example: -Dconfig.file=config.properties

  2. Check a predefined external location for a configuration file with a predefined name.

    For example: file:/etc/myapp/config.properties

  3. Use a default configuration file from the classpath.

    For example: classpath:com/myapp/default-config.properties

Upvotes: 2

Related Questions