簡體   English   中英

在 Maven 項目中使用 AspectJ 注釋:編織不起作用

[英]Using AspectJ annotations in maven project: weaving is not working

我試圖在不使用 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>

主應用程序

package com.pkg;

public class MainApp {

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

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

你好世界

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...");
   }

}

測試方面

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 文件,或嘗試加載時間編織)任何幫助表示贊賞。

你如何運行你的應用程序? 純粹來自 maven 還是來自 Eclipse? 你有 Eclipse 來自動構建你的項目嗎? 如果是,您可能不會在編譯時編織方面取得太大成功,因為 Eclipse 將使用 Eclipse 構建類覆蓋您的 Maven 構建類。 如果沒有安裝 AJDT 功能,並正確設置具有 AspectJ 性質的工作區項目,則生成的編譯代碼將不會被 AspectJ 編織器“增強”。

問題是 AspectJ Maven 和 Maven Compiler 的配置。 我的 AspectJ 的 POM 通常看起來與您的略有不同(更多設置),但這里是您的 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>

看看我如何將 Maven 編譯器的增量編譯設置為false 這是由於一個舊錯誤(仍未修復)實際上反轉了開關,因此為了使增量編譯工作,您必須“停用”它。 很奇怪。

您還需要為 AspectJ Maven 的process-sources階段定義執行。

此外,我升級到 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 插件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM