Aspectj does not work with kotlin

I want to use aspectj aop in kotlin, here is my code:

my annotation in annotation.lazy_list:

Kotlin:

 package anotation

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class lazy_list

my aspectj aop class:

@Aspect
class ActiveListAop{

    @Pointcut("execution(@annotation.lazy_list * *(..))")
    fun profile() {

    }

    @Before("profile()")
    fun testModeOnly(joinPoint: JoinPoint) {
        println("123")
    }

}

my use:

 @lazy_list
    fun all():List<T>{
        return lazy_obj?.all() as List<T>
    }

when I call the all () function, there is no error, but I do not print "123", why?

+4
source share
2 answers

For the annotation process in Kotlin, you must enable and use KAPT . Without this addition, through Kotlin's Gradle or Maven plugin to handle annotations in code would not work.

The Kotlin plugin supports annotation processors such as Dagger or DBFlow. For them to work with Kotlin classes, use the kotlin-kapt plugin.

See also:

+4

spring + kotlin + AOP , http://start.spring.io/ AOP, build.gradle ...

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')
    ...
}

kotlin- spring , AOP

@Aspect
@Component
class MyAspect {
...

: @Aspect @Component

!:)

0

Source: https://habr.com/ru/post/1678537/


All Articles