Spicy Spring: Using Groovy Configuration As PropertySource
We have many ways to provide configuration properties to a Spring (Boot) application.
We can add our own custom configuration properties format.
For example we can use Groovy's ConfigObject
object to set configuration properties.
We need to read a configuration file using ConfigSlurper
and make it available as a property source for Spring.
We need to implement two classes and add configuration file to support a Groovy configuration file in a Spring application.
First we need to write a class that extends the PropertySource
in the package org.springframework.core.env
.
This class has methods to get property values based on a given key.
There are already some subclasses for specific property sources.
There is for example also a MapPropertySource
class.
We will extend that class for our implementation, because we can pass our flattened ConfigObject
and rely on all existing functionality of the MapPropertySource
class: