Parse @PropertySource annotations on SpringApplication sources

If any of the sources has a @PropertySource annotation (or many)
then we can add those properties to the Environment. It's a nice
convenient way of specifying a custom external properties location
for an app.

One problem is that Spring will come along and parse the same
annotations later as part of the @Configuration parsing. The
user has pretty limited control over how that is done, and it
will never be done in a "natural" way for a Boot application
(which would prefer that the default application.properties
is applied *last*, whereas Spring will apply the @PropertySource
last).

To get round that problem we add the property sources with
a different name (key in the PropertySources in Environment),
prefixing named property sources with "boot.", and adding
others with a name that is the same as the resource location
(instead of its description, which is the default for
Spring).

Another problem is that Spring doesn't know about YAML, so
the user is currently restricted to using properties files
with this annotation.
This commit is contained in:
Dave Syer
2013-12-23 08:44:32 +00:00
parent 808daa54e5
commit bd0a499ab8
6 changed files with 234 additions and 15 deletions

View File

@@ -740,11 +740,23 @@ without changing the defaults.
## Change the Location of External Properties of an Application
Properties from different sources are added to the Spring
By default properties from different sources are added to the Spring
`Environment` in a defined order, and the precedence for resolution is
1) commandline, 2) filesystem (current working directory)
`application.properties`, 3) classpath `application.properties`. To
modify this you can provide System properties (or environment variables)
`application.properties`, 3) classpath `application.properties`.
A nice way to augment and modify this is to add `@PropertySource`
annotations to your application sources. Classes passed to the
`SpringApplication` static convenience methods, and those added using
`setSources()` are inspected to see if they have `@PropertySources`
and if they do those properties are added to the `Environment` early
enough to be used in all phases of the `ApplicationContext`
lifecycle. Properties added in this way have precendence over any
added using the default locations, but have lower priority than system
properties, environment variables or the command line.
You can also provide System properties (or environment variables) to
change the default behaviour:
* `config.name` (`CONFIG_NAME`), defaults to `application` as the root
of the file name