【问题标题】:AspectJ with Maven not woking带有 Maven 的 AspectJ 不工作
【发布时间】:2018-12-03 19:46:01
【问题描述】:

我正在尝试使用 IntelliJ 社区版中的 AssertJ

它没有按预期工作。我在哪里犯了错误?任何帮助/见解将不胜感激。

技术参考:

  1. IntelliJ IDEA 2018.2.4(社区版)

  2. java版本“1.8.0_77”


package org.kayd;

public class Client {
public static void main(String[] args) {
    Client data = new Client();
    data.data();
}

public void data() {
    System.out.println("kayd");
}
}

方面类

package org.kayd;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectTest {

    @Pointcut("execution(* *(..))")
    public void defineEntryPoint() {
    }

    @Before("defineEntryPoint()")
    public void log(JoinPoint joinPoint) {
        System.out.println("log");
    }

    @After("execution(org.kayd.Client.data())")
    public void after() {
        System.out.println("log");
    }

}

AOP.xml

<aspectj>
    <aspects>
        <aspect name="org.kayd.AspectTest"/>
    </aspects>
</aspectj>

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>org.kayd</groupId>
    <artifactId>AOP</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.9</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>1.8</source>
                    <target>1.8</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>${java.version}</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

截图

参考:我已经研究过这些问题,但仍然没有工作。

【问题讨论】:

  • 我还没有复制你的代码和 POM 并尝试编译它,但乍一看有两件事让我觉得很奇怪:你应用了编译时编织,但还提供了一个 aop。 xml 仅在加载时编织时需要。那你想走哪条路?对于 CTW,您可以将其删除。其次,切入点execution(org.kayd.Client.data()) 应该产生编译错误,因为语法无效(没有为方法签名指定返回类型)。您应该使用execution(* org.kayd.Client.data())execution(void org.kayd.Client.data()) 之类的东西。这有帮助吗?

标签: java aop aspectj aspect aspectj-maven-plugin


【解决方案1】:

我刚刚有一点时间,在我的机器上重新创建了您的 Maven 项目。我在评论中所说的实际上是真实的,所以我在这里引用自己以将其转化为答案:

乍一看,有两件事让我觉得很奇怪:

  • 您应用了编译时编织,但也提供了一个 aop.xml,它只在加载时编织时需要。那你想走哪条路?对于 CTW,您只需将其删除即可。
  • 其次,切入点execution(org.kayd.Client.data()) 应该产生编译错误,因为语法无效(没有为方法签名指定返回类型)。您应该使用execution(* org.kayd.Client.data())execution(void org.kayd.Client.data()) 之类的东西。

我想补充一点,不建议使用after 作为方法名,因为在 AspectJ 原生语法中它是保留关键字。编译器不会抱怨,但你还是要小心。

我像这样修改了你的方面以避免 Maven 编译错误,并在控制台上看到更多内容:

package org.kayd;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectTest {
  @Pointcut("execution(* *(..))")
  public void defineEntryPoint() {}

  @Before("defineEntryPoint()")
  public void log(JoinPoint joinPoint) {
    System.out.println("Before advice on " + joinPoint);
  }

  @After("execution(org.kayd.Client.data())")
  public void afterAdvice(JoinPoint joinPoint) {
    System.out.println("After advice on " + joinPoint);
  }
}

我还对您的 POM 进行了一些修改,因为一方面您使用的是最新的 AspectJ 版本,但旧版本的 AspectJ Maven 插件依赖于旧版本,这会导致 Maven 控制台上出现奇怪的日志输出。此外,我添加了一个 One-JAR 打包器和 Maven Exec 插件,因此您可以将整个应用程序放在一个 Über-JAR 中,并且可以在类路径上不使用任何其他内容的情况下运行它。

<?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>org.kayd</groupId>
  <artifactId>AOP</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.8</java.source-target.version>
    <aspectj.version>1.9.2</aspectj.version>
    <main-class>org.kayd.Client</main-class>
  </properties>

  <build>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
            <source>${java.source-target.version}</source>
            <target>${java.source-target.version}</target>
            <!-- IMPORTANT -->
            <useIncrementalCompilation>false</useIncrementalCompilation>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>aspectj-maven-plugin</artifactId>
          <version>1.11</version>
          <configuration>
            <!--<showWeaveInfo>true</showWeaveInfo>-->
            <source>${java.source-target.version}</source>
            <target>${java.source-target.version}</target>
            <Xlint>ignore</Xlint>
            <complianceLevel>${java.source-target.version}</complianceLevel>
            <encoding>${project.build.sourceEncoding}</encoding>
            <!--<verbose>true</verbose>-->
            <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
          </configuration>
          <executions>
            <execution>
              <!-- IMPORTANT -->
              <phase>process-sources</phase>
              <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
          <dependencies>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjtools</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjweaver</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
          </dependencies>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.4.0</version>
          <configuration>
            <mainClass>${main-class}</mainClass>
          </configuration>
        </plugin>
        <plugin>
          <groupId>com.jolira</groupId>
          <artifactId>onejar-maven-plugin</artifactId>
          <version>1.4.4</version>
          <executions>
            <execution>
              <goals>
                <goal>one-jar</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <onejarVersion>0.96</onejarVersion>
            <mainClass>de.scrum_master.app.FooBar</mainClass>
            <attachToBuild>true</attachToBuild>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>com.jolira</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <configuration>
          <mainClass>${main-class}</mainClass>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
      </plugin>
    </plugins>

  </build>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
  </dependencies>

</project>

然后执行mvn clean verify 之类的操作,然后执行这两个操作中的任何一个:

mvn exec:java

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< org.kayd:AOP >----------------------------
[INFO] Building AOP 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ AOP ---
Before advice on execution(void org.kayd.Client.main(String[]))
Before advice on execution(void org.kayd.Client.data())
kayd
After advice on execution(void org.kayd.Client.data())
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.043 s
[INFO] Finished at: 2018-12-12T12:07:59+07:00
[INFO] ------------------------------------------------------------------------

或者如果你想运行 Über-JAR:

java -jar target\AOP-1.0-SNAPSHOT.one-jar.jar

Before advice on execution(void org.kayd.Client.main(String[]))
Before advice on execution(void org.kayd.Client.data())
kayd
After advice on execution(void org.kayd.Client.data())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2018-02-20
    相关资源
    最近更新 更多