gpt4 book ai didi

java - AspectJ 与 Maven(仍然)无法工作

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:52 28 4
gpt4 key购买 nike

我正在尝试在 Eclipse Neon 中使用 aspectj 创建一个示例 maven 项目。然而,方面并没有编织/工作(参见下面的输出)。我尝试寻找很多原因和解决方案,但没有一个有效(请参阅下面的相关问题)。任何帮助/见解将非常感激。谢谢!

使用的技术:

  • Javajdk-1.8
  • Eclipse(Java EE)Neon 3
  • Apache Maven 3.5.0
  • junit-4.5(通过 maven 包含)
  • aspectjrt-1.8.9(通过 maven 包含)

(我还在我的 Eclipse 安装中安装了一个单独的项目的 AJDT 插件 - 不确定这是否会抑制 aspectj 的 Maven 存储库版本)

目录:

$ tree .
.
├── pom.xml
└── src
├── main
| └── java
| └── com
| └── hellomaven
| └── quickstart
| ├── App.java
| └── AppAspect.java
└── test
└── java
└── com
└── hellomaven
└── quickstart
└── AppTest.java

App.java

package com.hellomaven.quickstart;

public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
say("billy", "Nice to meet you");
Dog d = new Dog();
d.bark(2);
}

public static void say(String name, String word) {
System.out.println(name.toUpperCase() + " says " + word.toLowerCase() + "!");
}
}

class Dog {
Dog() {
System.out.println("..Dog init code..");
}

public void bark(int n) {
for (int i = 0; i < n; i++) {
System.out.print("bark ");
}
System.out.println("!");
}

}

AppAspect.java

package com.hellomaven.quickstart;

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

@Aspect
public class AppAspect {
@Before("execution(public static * *(..))")
public void testAspectBefore() {
System.out.println("Before Okay");
}

@Pointcut("execution(* *(..))")
public void testAspectPointcut() {
System.out.println("Pointcut Okay");
}

@After("execution(* *(..))")
public void testAspectAfter() {
System.out.println("After Okay");
}

@Around("execution(* *(..))")
public void testAspectAround() {
System.out.println("Around Okay");
}
}

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.hellomaven</groupId>
<artifactId>quickstart</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>

<name>quickstart</name>
<url>http://maven.apache.org</url>

<properties>
<java.version>1.8</java.version>
<aspectj.version>1.8.9</aspectj.version>
<junit.version>4.5</junit.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.9</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>${java.version}</source>
<target>${java.version}</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>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<mainClass>com.hellomaven.quickstart</mainClass>
</configuration>
</plugin>

</plugins>
</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

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

输出

<!-- language: lang-none -->
Hello World!
BILLY says nice to meet you!
..Dog init code..
bark bark !

我尝试过但失败的相关问题:

最佳答案

我不确定为什么,但在包含 aspectjweaver-1.8.9 依赖项、更新到 junit-4.12 并更改方面代码后,代码工作正常。以下是更新后的方面代码 (AppAspect.java):

package com.hellomaven.quickstart;

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 AppAspect {

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

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

@After("defineEntryPoint()")
public void bbb(JoinPoint joinPoint) {
System.out.println("aspect after");
}
}

这是新的依赖项:

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

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

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

关于java - AspectJ 与 Maven(仍然)无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491891/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com