gpt4 book ai didi

java - AspectJ 列出所有连接点

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:51 25 4
gpt4 key购买 nike

我想将 AspectJ 与 Java 结合使用来打印程序中所有连接点的列表。

我在 this document 的第二页上找到了一个旧代码示例.

public aspect Logging {
before (): !within (aspects.*) {
System.out.println(thisJoinPointStaticPart);
}
}

主类如下所示:

public class Main {
public static void main(String[] args) {
methodeA();
}

public static void methodeA(){
}
}

遗憾的是,如果我使用 Eclipse Luna Service Release 2 (4.4.2) 运行此代码,则会发生异常:

Exception in thread "main" java.lang.ExceptionInInitializerError
at Main.<clinit>(Main.java:1)
Caused by: org.aspectj.lang.NoAspectBoundException: Logging
at Logging.aspectOf(Logging.aj:1)
at Logging.<clinit>(Logging.aj)
... 1 more

我希望输出如下:

...
call(void Main.methodeA())
execution(void Main.methodeA())

最佳答案

可能是你的目录结构不对。您可以尝试使用以下小示例项目(基于您的代码)并可以通过 maven 执行。

假定的文件/目录结构

pom.xml
src/main/java/aspects/Logging.aj
src/main/java/Main.java

日志记录.aj

package aspects;
public aspect Logging {
before(): !within(aspects.*) {
System.out.println(thisJoinPointStaticPart);
}
}

主.java

public class Main {
public static void main(String[] args) {
methodeA();
}
public static void methodeA() {
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sub.optimal</groupId>
<artifactId>AspectJScratch</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

执行 mvn clean compile exec:java 将产生以下输出。

[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ AspectJScratch ---
staticinitialization(Main.<clinit>)
execution(void Main.main(String[]))
call(void Main.methodeA())
execution(void Main.methodeA())

关于java - AspectJ 列出所有连接点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34871320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com