Created
December 9, 2021 12:25
-
-
Save revseev/6f35175f0374947ead1f19f812536cc8 to your computer and use it in GitHub Desktop.
Externalized properties with Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.boot.context.properties.ConfigurationProperties | |
| import org.springframework.context.annotation.Configuration | |
| import org.springframework.context.annotation.PropertySource | |
| import kotlin.properties.Delegates | |
| @Configuration | |
| @PropertySource( | |
| value = ["\${app.processing.configuration}"], // file:/folder/processing-config.yml | |
| factory = YamlPropertySourceFactory::class, | |
| encoding = "UTF-8" | |
| ) | |
| @ConfigurationProperties(prefix = "apply") | |
| class ProcessingConfig { | |
| var retry: Retry = Retry() | |
| class Retry { | |
| var checkAfter: CheckAfter = CheckAfter() | |
| class CheckAfter { | |
| var statePositionMismatch by Delegates.notNull<Int>() | |
| } | |
| } | |
| } | |
| class YamlPropertySourceFactory : PropertySourceFactory { | |
| override fun createPropertySource(name: String?, encodedResource: EncodedResource): PropertySource<*> { | |
| val resource = encodedResource.resource | |
| val properties = YamlPropertiesFactoryBean().apply { | |
| setResources(resource) | |
| }.`object` | |
| return PropertiesPropertySource(requireNotNull(resource.filename), requireNotNull(properties)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment