4

我想配置 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 之类的注释会被忽略。我希望这些注释在同一个类中工作而无需自动装配。

4

1 回答 1

-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()
}
于 2021-06-24T19:32:09.993 回答