Skip to content

Support constructor binding on 3rd party classes #23172

@tomfi

Description

@tomfi

I use kotlin, and I want to use data classes to describe my properties/options. The data classes themselves sit in core modules that do not depend on spring, and should have minimal 3rd party dependencies.

On top of the modules mentioned above, I built spring auto-configuration modules that create beans, load properties, and utilize spring's features to expose the core module as proper spring modules.

It would be great if I could create a data class in my core module, e.g.

data class FooProperties(
  val bar: String
)

And then in the spring auto-configuration module somehow load it with constructor binding, e.g.

@Configuration
@EnableConfigurationProperties(value = [FooProperties::class], constructorBinding: true)
class FooAutoConfiguration

Or maybe some way to do it programmatically, e.g.

@Bean
fun fooProperties(binder: SpringConstructorBinder, environment: Environment): FooProperties {
  return binder.bind(environment, FooProperties::class)
}

I saw the following issues: #18935, #19011.
If i understand correctly i can create a bean that wraps my data class and the constructor binding will work for my inner class, but it will be nice to have a better way to define the properties without additional wrappers.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions