ajc可以省略指定的@Aspect类,让spring aop来处理吗?

问题描述 投票:0回答:1

我的应用程序需要同时使用

Aspectj
Spring AOP
,Spring AOP有时很方便,但是当我使用
ajc
编译器时,它会编译所有用
@Aspectj
注释的文件。

我的问题是:我可以告诉 ajc 忽略特定的

Aspectj
文件并让
Spring AOP
处理
aop
工作吗?我尽了最大努力,但没能弄清楚,所以需要帮助......谢谢提前!


我使用 gradle 来管理我的应用程序,并且我编写了一个 gradle 插件,其中集成了

ajc

weaveClasses.dependsOn compileJava
 classes.dependsOn weaveClasses

我使用后编译方法。

这是我的gradle配置,我只发布结构。

group 'com.test.api'
apply plugin: 'base'
apply plugin: 'maven-publish'
apply plugin: 'findbugs'

apply plugin: 'java'

sourceCompatibility = 1.8


ext {
    springBootVersion = '1.5.4.RELEASE'
    springVersion = '4.3.9.RELEASE'




    env = System.getProperty("env") == null ? "development" : System.getProperty("env")
}

buildscript {
    ext {
        springBootVersion = '1.5.3.RELEASE'
    }

    repositories {
        mavenLocal()
        jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
        classpath "com.test.tools:aspectj-post-compile:1.1.5"
    }
}

allprojects {
    apply plugin: 'java'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    version '0.1.7'
    group 'com.test.api'

    apply plugin: 'maven-publish'
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
            }
        }
    }
}


subprojects {
    apply plugin: 'idea'
    apply plugin: 'java'
    apply plugin: "aspectj.post.compile"

    version = parent.version
    group = parent.group

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    configurations {
        all*.exclude group: "org.springframework.boot", module: "spring-boot-starter-logging"
        all*.exclude group: "org.slf4j", module: "slf4j-log4j12"
    }

    apply plugin: 'checkstyle'
    checkstyle {
        toolVersion = "7.6"
        ignoreFailures = true
        configFile file("${project.rootDir}/checkstyle/test-java.xml")
    }

    tasks.withType(Checkstyle) {
        reports {
            xml.enabled = false
            html.enabled = false
        }
    }

    repositories {
        mavenLocal()
        maven { url "http://maven-test.corp.yiran.com:8081/repository/maven-public/" }
        jcenter()
    }

    sourceSets {
        main {
            resources {
                srcDirs = ["src/main/resources/base", "src/main/resources/$env"]
            }
        }
        test {
            resources {
                srcDirs = ["src/test/resources/base", "src/test/resources/$env"]
            }
        }
    }



    [compileJava, compileTestJava]*.options.collect { options ->
        options.compilerArgs.add '-parameters'
    }
    weaveClasses.dependsOn compileJava
    classes.dependsOn weaveClasses
}



project(":geneva-contract") {
    jar.archiveName = "test-contract.jar"

    dependencies {
        compile "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"
        compile "org.projectlombok:lombok:$lombokVersion"
        compile "com.test.service:farmer-base:$farmerVersion"
        compile "com.test.api:daily-open-base:$dailyOpenVersion"

    }
}
aop aspectj
1个回答
0
投票

最简单的方法是将 AspectJ 和 Spring AOP 方面放入不同的 Maven/Gradle 模块中,并且仅使用 AspectJ 编译器构建 AspectJ 模块,然后将该模块放在要编织的应用程序模块的方面路径上进入。

更复杂的方法是使用未记录的功能进行编译时编织:

-xmlConfigured /path/to/my/aop-ctw.xml

更新: 该功能现已记录在此处

另请参阅我的 AspectJ 票证 455014 并点击我的评论中的链接以获取更多信息。

一个小例子:

package de.scrum_master.app;

public class Application {
  public static void main(String[] args) {}
}
package de.scrum_master.aspect;

public aspect FirstAspect {
  before() : execution(* *(..)) {
    System.out.println(this.getClass().getSimpleName() + ": " + thisJoinPoint);
  }
}
package de.scrum_master.aspect;

public aspect SecondAspect {
  before() : execution(* *(..)) {
    System.out.println(this.getClass().getSimpleName() + ": " + thisJoinPoint);
  }
}

如果正常编译并运行,控制台日志显示:

FirstAspect: execution(void de.scrum_master.app.Application.main(String[]))
SecondAspect: execution(void de.scrum_master.app.Application.main(String[]))

但是如果你添加这样的配置文件...

<aspectj>
  <aspects>
    <aspect name="de.scrum_master.aspect.FirstAspect" />
  </aspects>
</aspectj>

...然后从编译器命令行引用它,例如就像这样在安装了 AJDT 的 Eclipse 中,...

...控制台日志更改为:

FirstAspect: execution(void de.scrum_master.app.Application.main(String[]))
© www.soinside.com 2019 - 2024. All rights reserved.