带有Kotlin和Gradle的Spring AOP(AspectJ)-我无法使它正常工作 [英] Spring AOP (AspectJ) with Kotlin and Gradle - I can't get it to work

查看:303
本文介绍了带有Kotlin和Gradle的Spring AOP(AspectJ)-我无法使它正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot + Kotlin + Gradle项目.我想为我的用例创建一个小型库.该库应使用AOP消除我观察到的一些横切关注点.

I have a Spring Boot + Kotlin + Gradle project. I'd like to create a small library for my use-cases. This library should use AOP to remove some cross cutting concerns I observed.

因此,我开始将这两个依赖项添加到我的gradle构建文件中.

Therefore I started adding these two dependencies to my gradle build file.

build.gradle.kts

implementation("org.springframework:spring-aop:5.2.9.RELEASE")
implementation("org.springframework:spring-aspects:5.2.9.RELEASE")

由于互联网的一些建议,我还添加了freefair Aspectj插件. 根据此文档,应将我创建的以下方面放在src/main/aspectj中:

I also added the freefair aspectj plugin due some suggestions from the interwebs. The following aspect I created should be placed in src/main/aspectj according to this documentation: https://docs.freefair.io/gradle-plugins/5.2.1/reference/#_io_freefair_aspectj

此插件通过向每个源集中添加一个Aspectj目录,从而为项目增加了AspectJ支持. src/main/aspectj目录中包含的源将由compileAspectj任务使用ajc进行编译.

This plugin adds AspectJ support to the project, by adding a aspectj directory to every source set. Source contained in the src/main/aspectj directory will be compiled with ajc by the compileAspectj task.

plugins {
    // ...
    id("io.freefair.aspectj") version "5.2.1"
    // ...
}

然后我开始创建我的第一个方面,该方面与每个使用@Foozy

I then started to create my first aspect that matches on every method which is annotated with @Foozy

src/main/aspectj/FoozyAspect.kt< 特殊"源路径

src/main/aspectj/FoozyAspect.kt < the 'special' source path

@Component
@Aspect
class FoozyAspect {

    @Before("@annotation(com.client.annotation.Foozy)")
    fun doStuff() {
        LOG.info("Do Stuff")
    }

    companion object {
        private val LOG = LoggerFactory.getLogger(FoozyAspect::class.java)
    }
}

然后我创建了此批注

src/main/kotlin/com.client.annotation/Foozy.kt

@Target(AnnotationTarget.FUNCTION)
annotation class Foozy

现在要测试是否一切正常,我创建了单元测试

Now to test if everything works as expected I created a unit test

src/test/kotlin/FoozyAspectTest.kt

@SpringBootTest
@EnableAspectJAutoProxy
internal class FoozyAspectTest {
    private val testCandidate: TestCandidate = TestCandidate()

    @Test
    fun `should work with aspect`() {
        testCandidate.doStuff()
    }
}

src/test/TestCandidate.kt

class TestCandidate {
    @Foozy
    fun doStuff(): String {
        return "stuff"
    }
}

结果

在调试模式下执行文本不会产生等待的信息日志Do Stuff,也不会在FoozyAspect.kt doStuff()方法中的断点处停止线程. 我不知道在这里配置什么. 出于充分的理由,我有点怀疑我正在混淆不同的方式".使其无法正常工作,或者只是错过了预配置/先决条件中的某些最后步骤.

Result

Executing the text in debug mode does not yield the awaited info log Do Stuff and also does not cease the thread at the breakpoint in the FoozyAspect.kt doStuff() method. I have no idea what to configure here. For good reason I kinda have the suspicion that I am mixing up different "ways" to get this to work or am just missing some final steps in preconfiguration/prerequisites.

推荐答案

AspectJ编译器无法编译Kotlin源代码.您在src/main/aspectj中的.kt文件将被完全忽略.

The AspectJ compiler can't compile Kotlin source code. Your .kt file in src/main/aspectj will be completely ignored.

根据您真正想做什么,您有不同的选择:

You have different options depending on what you really want to do:

您是要在编译时由acc编织Aspect还是只想使用纯"春季AOP?

Do you want your Aspect to be woven by ajc at compile-time, or do you just want to use "plain" Spring AOP?

此处说明了差异: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-choosing

如果您只想使用Spring AOP,则不需要特殊的gradle插件.只需将您的.kt文件放在src/main/kotlin中,并遵循Spring AOP文档.

If you just want to use Spring AOP, you dont need a special gradle plugin. Just put your .kt file in src/main/kotlin and follow the Spring AOP docs.

如果要在编译时使用ajc编织方面,则有两个选择:

If you want to weave your aspect at compile-time with ajc, you have two options:

  1. 保持io.freefair.aspectj插件一步编译一个方面:将您的方面实现为.java.aj,以便可以由ajc进行编译
  2. 切换到io.freefair.aspectj.post-compile-weaving插件,以分开编译步骤:在这种情况下,您可以将Aspect实现保留为Kotlin,但必须将其放在src/main/kotlin中.然后,您的Aspect将由kotlinc编译,然后由ajc编制.
  1. Keep the io.freefair.aspectj plugin to compile an weave the aspect in one step: Implement your aspect as .java or .aj so it can be compiled by ajc
  2. Switch to the io.freefair.aspectj.post-compile-weaving plugin in order to separate the compilation an weaving steps: In this case you can keep your Aspect implementation as Kotlin, but you have to put it in src/main/kotlin. Your Aspect will then be compiled by kotlinc and then woven by ajc.

这篇关于带有Kotlin和Gradle的Spring AOP(AspectJ)-我无法使它正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆