Spring AOP injection (I’m not sure if this term is correct, see example below) never works in Kotlin (IntelliJ IDEA 2017.2.3).
Example in Java:
The same in Kotlin:
Spring AOP injection (I’m not sure if this term is correct, see example below) never works in Kotlin (IntelliJ IDEA 2017.2.3).
Example in Java:
The same in Kotlin:
Did you use kotlin-spring compiler plugin? By default all Kotlin classes are final and for AOP to work they must not be final.
You also need to mark your method as open
. Kotlin plugin usage described here.
You need use gradle plugin:
https://kotlinlang.org/docs/tutorials/spring-boot-restful.html
We might be talking about absolutely different things. The problem is the injection in COMPILE time (see java screen-shot above), not RUNTIME. And in compile time the injection does not work. ‘open/final’ makes no difference.
spring + kotlin + AOP work nice, just go to http://start.spring.io/ and generate a project with AOP support, you can see a piece of build.gradle here…
buildscript {
ext {
kotlinVersion = '1.2.30'
springBootVersion = '2.0.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'org.springframework.boot'
...
dependencies {
compile('org.springframework.boot:spring-boot-starter-aop')
...
}
plugin kotlin-spring makes all classes open to allow AOP
Then, just declare your aspect as follows
@Aspect
@Component
class MyAspect {
...
Important: annotate your aspect class with @Aspect and @Component annotations
Piece of cake!
So, why don’t you show a screenshot with working injection?
I faced the same error, when I tried to use AOP with Kotlin, just created two classes.
With Java code compile-time dependencies works fine,
for Kotlin - no luck
I was facing the same problem with my project. I created a sample project and AOP worked with Kotlin . But not with my original project, as exposed by sva605.
So I included @Order(Ordered.HIGHEST_PRECEDENCE + 1) annotation on my aspect implementation and it worked.