You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin多模块Gradle项目中Freefair AspectJ切面不生效问题求助

问题分析与解决方案

在Kotlin-Gradle多模块SpringBoot项目中使用io.freefair.aspectj.post-compile-weaving时切面失效,核心问题出在模块插件配置不合理和Spring组件扫描范围缺失两个方面,以下是针对性修复方案:

1. 调整根项目Gradle配置,拆分插件应用范围

原配置在allprojects中给所有模块都应用了org.springframework.boot插件,导致common模块被打包为Spring Boot可执行Jar(而非普通库Jar),依赖时无法正确加载切面类。修改根项目build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.1.5" apply false
    id("io.spring.dependency-management") version "1.1.3" apply false
    id("io.freefair.aspectj.post-compile-weaving") version "6.6.3" apply false
    kotlin("jvm") version "1.8.22" apply false
    kotlin("plugin.spring") version "1.8.22" apply false
}

group = "com.voiddev"

subprojects {
    apply(plugin = "org.jetbrains.kotlin.jvm")
    apply(plugin = "kotlin-spring")
    apply(plugin = "io.spring.dependency-management")
    apply(plugin = "io.freefair.aspectj.post-compile-weaving")

    version = "0.0.1-SNAPSHOT"
    java {
        sourceCompatibility = JavaVersion.VERSION_17
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
        implementation("org.jetbrains.kotlin:kotlin-reflect")
        testImplementation("org.springframework.boot:spring-boot-starter-test")

        implementation("org.aspectj:aspectjrt:1.9.19")
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs += "-Xjsr305=strict"
            jvmTarget = "17"
        }
    }

    tasks.withType<Test> {
        useJUnitPlatform()
    }
}

2. 配置common模块为纯库模块

common模块作为切面定义模块,不需要Spring Boot可执行Jar能力,修改其build.gradle.kts:

plugins {
    id("io.spring.dependency-management")
}

dependencies {
    // 仅引入Spring基础依赖,无需web starter
    implementation("org.springframework.boot:spring-boot-starter")
}

同时确保切面类添加@Component和@Aspect注解,让Spring能识别并管理它:

package com.voiddev.common.aspect

import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.springframework.stereotype.Component

@Aspect
@Component
class LoggingAspect {
    @Before("execution(* com.voiddev.server.controller..*(..))")
    fun logBefore() {
        println("Aspect triggered: Before method execution")
    }
}

3. 修复server模块配置与组件扫描

server模块作为Spring Boot启动模块,需单独应用Spring Boot插件,并配置组件扫描范围覆盖common模块:

修改server模块build.gradle.kts:

plugins {
    id("org.springframework.boot")
    id("io.spring.dependency-management")
    id("io.freefair.aspectj.post-compile-weaving")
    kotlin("jvm")
    kotlin("plugin.spring")
}

group = "com.voiddev"

dependencies {
    implementation(project(":common"))
    implementation("org.springframework.boot:spring-boot-starter-web")
}

tasks {
    bootJar {
        archiveBaseName.set(rootProject.name)
    }
}

// 可选:添加织入日志,用于调试切面是否生效
aspectj {
    weaverOptions.add("-showWeaveInfo")
}

在server模块的启动类中扩展组件扫描范围:

package com.voiddev.server

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.ComponentScan

@SpringBootApplication
@ComponentScan(basePackages = ["com.voiddev.common", "com.voiddev.server"])
class ServerApplication

fun main(args: Array<String>) {
    runApplication<ServerApplication>(*args)
}

核心修复逻辑

  1. 拆分Spring Boot插件应用范围:仅让启动模块(server)生成可执行Jar,common模块保持普通库Jar结构,确保依赖能正确加载切面类。
  2. 统一Post-compile-weaving插件应用:所有模块都启用织入能力,确保server模块编译时能将common模块的切面织入自身Bean。
  3. 完善Spring组件扫描:让Spring能扫描到common模块中的切面类,完成切面的注册与生效。

内容的提问来源于stack exchange,提问作者L_Cleo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 02:57:48