【发布时间】:2018-12-03 19:46:01
【问题描述】:
我正在尝试使用 IntelliJ 社区版中的 AssertJ。
它没有按预期工作。我在哪里犯了错误?任何帮助/见解将不胜感激。
技术参考:
IntelliJ IDEA 2018.2.4(社区版)
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