Search code examples
springdependency-injectionkotlinaspect

Kotlin Spring class initialization with aspect


I'm trying to use kotlin in my java8 spring project. I'm doing it by replacing classes (java->kotlin) one by one.

One of my class in Finder:

Finder.java has such structure:

@Compoment
class Finder {
    private SomeObject someObject;

    Finder() {
        someObject = new SomeObject();
    }

    public void doSomething() { //aspect looks here
        someObject.do();
    }
}

I've replaced it by Finder.kt

@Compoment
open public class Finder {
    private val someObject : SomeObject

    constructor() {
        someObject = SomeObject()
    }

    public fun doSomething() {  //aspect looks here
        someObject.do() //NPE here
    }

}

While debuggind, I've found, that constuctor was called, someObject was created when Finder instance was creating. But FinderEnhancerBySpring generated class instance was autowired to Detector instance. It was not initiliazed, so i've got NPE when I try to access to someObject.

Also Finder class has other autowired fields (to simplify code I haven't wrote it here), they were not initiliazed also.

UPD: I've found the aspect on the one of Finder's method. When I deleted it, type the autowired instance became Finder (not FinderEnhancerBySpring) and fully initiliazed.

What can be wrong here? (0.13.1514 - kotlin version)


Solution

  • All kotlin methods are final (speaking java) by default, so I've to allow overide it with open keyword:

    public open fun doSomething() {  //aspect looks here
        someObject.do()
    }