Is it possible to annotate the class constructor in Kotlin

Explanation These questions were asked before kotlin hit version 1.0. The language syntax is currently out of date, please follow the official docs.


I play with kotlin and spring DI . I want to use constructor-based dependency injection, so I need to annotate the constructor.

I tried the 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 } 

}

But that will not work. Is it even possible to annotate a constructor in kotlin?

PS I am using Kotlin M10.1 and Spring 4.1.4

UPDATE: An annotating constructor is possible in kotlin. The problem was that he was not allowed to use constructor-based DI for @Configuration

+7
annotations kotlin
source share
3 answers

Hrm, I think the syntax has changed radically since this question was posted. The current way (according to docs ) is to add the constructor keyword between your class name and arguments and annotate this, i.e.

 public class AppConfig @Configuration constructor(dataSource: DataSource) { //... } 
+16
source share

Try to write:

 Configuration public open class AppConfig [Import(javaClass<DataSourceConfig>())] (dataSource: DataSource) { //... } 
+1
source share

This syntax works for me:

 Configuration Import(javaClass<DataSourceConfig>()) public open class AppConfig { private val dataSource: DataSource Autowired constructor(dataSource: DataSource){ this.dataSource = dataSource } } 
0
source share

All Articles