Clarification
This questions was asked before kotlin
hit version 1.0. Language syntax in example is obsolete now, please follow official docs.
I'm playing with kotlin and spring DI. I want to use constructor-based dependency injection, so I need to annotate the constructor.
I tried following approach:
Configuration
Import(javaClass<DataSourceConfig>())
public open class AppConfig(dataSource: DataSource) {
private val dataSource: DataSource
Autowired {
this.dataSource = dataSource
}
}
Configuration
public open class DataSourceConfig {
Bean
public open fun dataSource(): DataSource {
// source omitted
}
}
P.S. I'm using Kotlin M10.1 and Spring 4.1.4
UPDATE: Annotating constructor is possible in kotlin. The problem was that it's not allowed to use constructor-based DI for @Configuration
Hrm, I think the syntax has changed radically since this question was posted. The current way (according to the docs) is to add the keyword constructor
between your class name and arguments and annotate that, i.e.
public class AppConfig @Configuration constructor(dataSource: DataSource) {
//...
}
Try to write:
Configuration
public open class AppConfig [Import(javaClass<DataSourceConfig>())] (dataSource: DataSource) {
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With