Maven的AspectJ不能正常工作

时间:2018-12-03 19:46:01

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

我正在尝试使用 IntelliJ Community Edition 中的 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>

屏幕截图

Project Structure

参考:我已经研究了这些问题,但仍然无法解决问题。

1 个答案:

答案 0 :(得分: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);
  }
}

由于您一方面使用了最新的AspectJ版本,但是我还对您的POM做了一些表述,但是旧版本的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())