【问题标题】:How to achieve AspectJ compile time weaving using Spring BOOT and Gradle in an multi module project(weaving all modules)?如何在多模块项目(编织所有模块)中使用 Spring BOOT 和 Gradle 实现 AspectJ 编译时编织?
【发布时间】:2019-11-24 22:19:13
【问题描述】:

我想配置 Spring BOOT 以使用 AspectJ 的编译时编织。 我面临的基本问题是,如果从同一个对象中调用注释函数,则会忽略 @Transactional、@Cacheable 之类的注释。我知道自动装配是一种解决方法,但这对我来说似乎是一种设计解决方法,我对此并不满意。

我尝试了 io.freefair(io.freefair.aspectj.post-compile-weaving) 的 gradle 插件。 我再次通过 @EnableCaching(mode=AdviceMode.ASPECTJ) 注释 springBootMain 将 Spring Boot 配置为使用 AspectJ。

请为我提供正确配置的进一步步骤。我应该如何告诉方面来编织这个模块或包?如何告诉 Spring AOP 不要编织已经编织的类?

我正在添加我的 build.gradle。

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "io.freefair.gradle:aspectj-plugin:3.8.0"
    }
}


allprojects {
    group 'com.XYZ.PQR'
    version '1.0-SNAPSHOT'
}

subprojects {


    repositories {
        mavenCentral()
        jcenter()
    }
    apply plugin: 'java'
    apply plugin: 'idea'

    apply plugin: 'io.freefair.aspectj.post-compile-weaving'


    dependencies {
        compileOnly 'org.projectlombok:lombok:1.18.6'
        annotationProcessor 'org.projectlombok:lombok:1.18.6'

        implementation 'org.springframework.boot:spring-boot-starter-web'


        compile group: 'org.springframework', name: 'spring-aspects', version: '5.1.8.RELEASE'

        compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.4'

        compile group: 'org.aspectj', name: 'aspectjrt', version: '1.9.4'

        testImplementation 'org.springframework.boot:spring-boot-starter-test'

        compile localGroovy()

    }
}

Spring BOOT 主程序

@SpringBootApplication
@EnableCaching(mode = AdviceMode.ASPECTJ)
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

如果在同一个类中调用带注释的函数,则 @Transactional 和 @Cacheable 之类的注释会被忽略。 我希望这些注释在同一个类中工作而无需自动装配。

【问题讨论】:

    标签: spring-boot gradle aspectj gradle-plugin compile-time-weaving


    【解决方案1】:
    plugins {
        id 'org.springframework.boot' version '2.5.1'
        id 'io.spring.dependency-management' version '1.0.11.RELEASE'
        id 'java'
    }
    group = 'com.yourcompany'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    def targetEnvironment = 'dev'
    
    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }
    
    repositories {
        mavenCentral()
        maven {
            url "https://repo.spring.io/release/"
        }
    }
    
    bootRun {
        systemProperties = System.properties
        systemProperty "spring.profiles.include", "${targetEnvironment},{targetEnvironment}-encrypted,encrypted"
    }
    
    task bootRunLocal() {
        dependsOn
        group 'application'
    
        doFirst {
            bootRun.configure {
                systemProperty 'user.dir', projectDir // Local data relies on relative path, so set pwd to projectDir
                systemProperty "spring.profiles.include", "${targetEnvironment},${targetEnvironment}-encrypted,local"
            }
        }
    
        finalizedBy bootRun
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-validation'
        implementation 'org.springframework.boot:spring-boot-starter-aop'
    
        implementation group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.0.RELEASE'
    }
    
    test {
        useJUnitPlatform()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多