-1

I try to implement method injection using Spring 5.1.6 and Kotlin 1.3.20

When i implement method injection using Kotlin method getNotification isn't return new object of SchoolNotification. I get 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!!
    }
 }

Everything is ok when i write the same code with 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()
}

What am i doing wrong using Kotlin?

Artyom Karnov
  • 557
  • 9
  • 24

1 Answers1

3

In addition to making your class open, you need to make your method open too, otherwise the method is still marked as final and Spring won't be able to provide its own implementation for it in a subclass:

@Lookup
open fun getNotification(): SchoolNotification {
    return null!!
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79