I would really like to use YAML config for Spring Boot, as I find it quite readable and useful to have a single file showing what properties are active in my different profiles. Unfortunately, I'm finding that setting properties in application.yml
can be rather fragile.
Things like using a tab instead of spaces will cause properties to not exist (without warnings as far as I can see), and all too often I find that my active profiles are not being set, due to some unknown issue with my YAML.
So I was wondering whether there are any hooks that would enable me to get hold of the currently active profiles and properties, so that I could log them.
Similarly, is there a way to cause start-up to fail if the application.yml
contains errors? Either that or a means for me to validate the YAML myself, so that I could kill the start-up process.
If
application.yml
contains errors it will cause a failure on startup. I guess it depends what you mean by "error" though. Certainly it will fail if the YAML is not well formed. Also if you are setting@ConfigurationProperties
that are marked asignoreInvalidFields=true
for instance, or if you set a value that cannot be converted. That's a pretty wide range of errors.The active profiles will probably be logged on startup by the
Environment
implementation (but in any case it's easy for you to grab that and log it in your launcher code - thetoString()
of tehEnvironment
will list the active profiles I think). Active profiles (and more) are also available in the /env endpoint if you add the Actuator.In addition to other answers: logging active properties on context refreshed event.
Java 8
Kotlin
Output like:
In case you want to get the active profiles before initializing the beans/application, the only way I found is registering a custom Banner in your SpringBootServletInitializer/SpringApplication (i.e. ApplicationWebXml in a JHipster application).
e.g.
I had the same problem, and wish there was a debug flag that would tell the profile processing system to spit out some useful logging. One possible way of doing it would be to register an event listener for your application context, and print out the profiles from the environment. I haven't tried doing it this way myself, so your mileage may vary. I think maybe something like what's outlined here:
How to add a hook to the application context initialization event?
Then you'd do something like this in your listener:
Might be worth a try. Another way you could probably do it would be to declare the Environment to be injected in the code where you need to print the profiles. I.e.:
Actuator /env service displays properties, but it doesn't displays which property value is actually active. Very often you may want to override your application properties with
Thus you will have the same property and different values in several sources.
Snippet bellow prints active application properties values on startup: