Почему инъекция пружинного метода не работает в Котлине? - PullRequest
0 голосов
/ 10 мая 2019

Я пытаюсь реализовать внедрение метода с использованием Spring 5.1.6 и Kotlin 1.3.20

Когда я реализую внедрение метода с использованием Kotlin, метод getNotification не возвращает новый объект SchoolNotification.Я получаю Kotlin NPE.

@Component
@Scope("prototype")
open class SchoolNotification {
override fun toString(): String {
    return Objects.toString(this)
    }
}

StudentServices:

@Component
open class StudentServices {
@Lookup
fun getNotification(): SchoolNotification {
    return null!!
    }
 }

Все нормально, когда я пишу тот же код на Java

@Component
@Scope("prototype")
public class SchoolNotification {

}

StudentServices:

@Component
public class StudentServices {

@Lookup
public SchoolNotification getNotification() {
    return null;
    }
}

Main

fun main() {
  val ctx = AnnotationConfigApplicationContext(ReplacingLookupConfig::class.java)
  val bean = ctx.getBean(StudentServices::class.java)
  println(bean.getNotification())
  println(bean.getNotification())
  ctx.close()
}

Что я делаю не так, используя Kotlin?

1 Ответ

2 голосов
/ 10 мая 2019

В дополнение к созданию вашего класса open, вам также нужно сделать ваш метод open, в противном случае метод по-прежнему помечен как final, и Spring не сможет предоставить свою собственную реализацию для него в подкласс:

@Lookup
open fun getNotification(): SchoolNotification {
    return null!!
}
...