1

我试图在不使用 Spring 的简单项目中使用 AspectJ,虽然我看到了类似的问题并且我的代码似乎是正确的,但我不明白为什么它不起作用。我使用的是Eclipse Oxygen 4.7.3(不使用AJDT工具)、JDK 7、maven 3.5.2,我的代码如下:

pom.xml

    <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>com</groupId>
  <artifactId>aspect-tutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>


<properties>
    <maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.8</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>

        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

    </plugins>
</build>
</project>

主应用程序.java

package com.pkg;

public class MainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        HelloWorld a = new HelloWorld();
        a.printHello();
    }
}

HelloWorld.java

package com.pkg;

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void printHello() {
        System.out.println("Print Hello...");
   }

}

TestAspect.java

package com.pkg;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;


@Aspect
public class TestAspect {       

    @Before("execution(* com.pkg.HelloWorld.printHello(..))")
    public void testBefore2(){
        System.out.println("Yeeha");
    }


}

运行 mvn clean install 成功,但输出只打印“Print Hello...”部分。我应该使用不同的方法吗?(也许改用 .aj 文件,或尝试加载时间编织)任何帮助表示赞赏。

4

2 回答 2

2

你如何运行你的应用程序?纯粹来自 Maven 还是来自 Eclipse?你有 Eclipse 来自动构建你的项目吗?如果是,您可能不会在编译时编织方面取得太大成功,因为 Eclipse 会用 Eclipse 构建的类覆盖您的 Maven 构建的类。如果没有安装 AJDT 功能,并且没有正确设置具有 AspectJ 特性的工作区项目,则生成的编译代码不会被 AspectJ 编织器“增强”。

于 2018-05-08T23:10:23.033 回答
2

问题在于 AspectJ Maven 和 Maven 编译器的配置。我的 AspectJ 的 POM 通常看起来和你的有点不同(更多的设置),但这里是你的,为了让它工作,做了最小的改变:

<?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>com</groupId>
  <artifactId>aspect-tutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.13</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.11</version>
        <configuration>
          <complianceLevel>1.7</complianceLevel>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
        <executions>
          <execution>
            <!-- IMPORTANT -->
            <phase>process-sources</phase>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven.compiler.plugin.version}</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <!-- IMPORTANT -->
          <useIncrementalCompilation>false</useIncrementalCompilation>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
        <configuration>
          <mainClass>com.pkg.MainApp</mainClass>
        </configuration>
      </plugin>

    </plugins>
  </build>
</project>

看看我如何false为 Maven 编译器设置增量编译?这是由于一个旧错误(仍未修复)实际上反转了开关,因此为了使增量编译工作,您必须“停用”它。很奇怪。

您还需要process-sources为 AspectJ Maven 的阶段定义执行。

此外,我升级到了 AspectJ Maven 1.11,因此也升级到了 AspectJ 运行时 1.8.13。

我还添加了 Maven Exec 插件,以便轻松证明它现在可以工作。只需调用mvn clean compile exec:java并检查输出:

(...)
[INFO] --- aspectj-maven-plugin:1.11:compile (default) @ aspect-tutorial ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ aspect-tutorial ---
[INFO] Nothing to compile - all classes are up to date
(...)
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ aspect-tutorial ---
Yeeha
Print Hello...
(...)

否则,我支持 Nándor 所说的:如果您还想从 IDE 运行方面增强的 Java 代码,请确保使用 Eclipse 或 IDEA 的 AspectJ 插件。

于 2018-05-09T01:17:23.747 回答