Skip to content

Instantly share code, notes, and snippets.

@revseev
Created December 9, 2021 12:25
Show Gist options
  • Select an option

  • Save revseev/6f35175f0374947ead1f19f812536cc8 to your computer and use it in GitHub Desktop.

Select an option

Save revseev/6f35175f0374947ead1f19f812536cc8 to your computer and use it in GitHub Desktop.
Externalized properties with Kotlin
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