4

I want to configure Spring BOOT to use AspectJ's compile time weaving. The base problem that I face is annotations like @Transactional, @Cacheable are ignored if the annotation function is called from within the same object. I know self autowiring is a fix, but that seems to be a design workaround for me and I am not satisfied with it.

I have tried a gradle plugin from io.freefair(io.freefair.aspectj.post-compile-weaving). Again I have configured Spring boot to use AspectJ by annotating springBootMain by @EnableCaching(mode=AdviceMode.ASPECTJ).

Kindly provide me further steps to configure properly. How should I tell aspect to weave this modules or packages? How to tell Spring AOP to not weave already weaved classes?

I am adding my 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 Main

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

Annotations like @Transactional and @Cacheable are getting igonred if the annotated function is called within the same class. I will like to these annotations to work within same class without self autowiring.

Aditya Kumar
  • 133
  • 1
  • 2
  • 11

1 Answers1

-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()
}