I'm trying to use spring-boot's configuration meta...
# spring
c
I'm trying to use spring-boot's configuration meta-data with spring boot 2.1.6 and kotlin: build.gradle.kts
Copy code
plugins {
	id("org.springframework.boot") version "2.1.6.RELEASE"
	id("io.spring.dependency-management") version "1.0.7.RELEASE"
	val kotlinVersion = "1.3.41"
	kotlin("jvm") version kotlinVersion
	kotlin("plugin.spring") version kotlinVersion
	kotlin("kapt") version kotlinVersion
}
dependencies {
	kapt("org.springframework.boot:spring-boot-configuration-processor")
	implementation(kotlin("reflect"))
	implementation(kotlin("stdlib-jdk8"))
	implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
	//...
}
application.yml
Copy code
security:
  csrfEnabled: true
  token.writer.secret: h2l89efhlwskiehf89o23fhnsldhf89023
usage of
secret
in a class:
Copy code
@ConfigurationProperties(prefix = "security.token.writer")
class TokenBasedAuthenticationFilter {
	private lateinit var secret: String
	//...
There is no metadata or autocomplete and IntelliJ shows
Cannot resolve configuration property 'security.token.writer.secret'
warning Am I missing something or is it a bug/limitation?
l
works for me
look at 50.5
c
Huh, maybe it's kotlin-reflect missing, I don't remember and the project is on my work laptop, I'll check it tomorrow. Thanks for the pointer.
Nope, I didn't forget
kotlin-reflect
, it's there. I also re-checked all the requirements in 50.1, all are satisfied. And 50.5 only asks to do what I already have shown in the snippets above. I'll edit and add relevant dependencies to the snippet, to avoid ambiguity.
Half the problems solved, turns out it is a limitation on the IntelliJ side, which doesn't see the bindings, until I compile the code, then all the bindings are visible. Now I have another problem, nested configurations do not work.
Copy code
@ConfigurationProperties(prefix = "security")
class SecurityConfigurationParameters {
	var csrfEnabled: Boolean = true
	@NestedConfigurationProperty
	lateinit var token: Token

}

class Token {
	lateinit var privateSecret: String
	lateinit var publicSecret: String
}
I also tried making
Token
an
inner class Token
in the
SecurityConfigurationProperties
- to no avail. Error is always:
Copy code
BindException: Failed to bind properties under 'security.token' to com.example.security.Token
IllegalStateException: Unable to get value for property token
It just doesn't work... To hell with it, normal immutable stuff is fully supported and works (see code below) on 2.2 M4, I'll use that. For the project I'm working on production isn't planned till October anyway 😃
Copy code
@ConfigurationProperties(prefix = "security")
class SecurityConfigurationParameters(
	@DefaultValue("false")
	val csrfEnabled: Boolean,
	@NestedConfigurationProperty
	val token: TokenConfig
)

class TokenConfig(
	val privateSecret: String,
	val publicSecret: String
)
226 Views