AOP injection never works


#1

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:


#2

Did you use kotlin-spring compiler plugin? By default all Kotlin classes are final and for AOP to work they must not be final.


#3

This plugin is used:
plugin

Final or open class — it still makes no difference.


#4

You also need to mark your method as open. Kotlin plugin usage described here.


#5

You need use gradle plugin:


#6

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.


#7

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! :slight_smile:


#8

So, why don’t you show a screenshot with working injection?


#9

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


#10

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.