问题描述
我正在尝试创建一个从另一个项目(testMaven)调用的项目方法(test5)的建议。我已经将test5作为依赖项包含在testMaven的pom.xml中,并在testMaven的类中设置了一个建议,但仍然没有被执行。调用方法(dfg)正常工作。
I am trying to create an advice of a method of a project (test5) being called from another project (testMaven). I already included the project test5 as dependency in testMaven's pom.xml and set up an advice in testMaven's class but still it doesn't get executed. The calling of the method (dfg) is working just fine.
以下是其代码:
MainApp.java(testMaven)
MainApp.java (testMaven)
package testMaven;
import test5.yxc;
public class MainApp {
public static void main(String[] args) {
yxc b = new yxc();
b.dfg(2);
}
}
yxc.java(test5)
yxc.java (test5)
package test5;
public class yxc {
public void dfg(int a){
System.out.println(a);
}
}
testAspect.java(testMaven) / p>
testAspect.java (testMaven)
package testMaven;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import test5.yxc;
@Aspect
public class testAspect {
@Before("execution(* test5.yxc.dfg(..))")
public void testBefore(){
System.out.println("yooi");
}
}
pom.xml(testMaven)
pom.xml(testMaven)
<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>testingMaven</groupId>
<artifactId>testMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<java.version>1.8</java.version>
<!-- Maven Plugin Versions -->
<maven.compiler.plugin.version>3.2</maven.compiler.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>test5</groupId>
<artifactId>test5</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
你能帮我一下吗?谢谢
推荐答案
在,并检出代码并更改了POM,如下所示:
Checked your code in testMaven and checked out the code and changed the POM as follows:
<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>testMaven</groupId>
<artifactId>testMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<aspectj.version>1.8.9</aspectj.version>
<!-- Maven Plugin Versions -->
<maven.compiler.plugin.version>3.2</maven.compiler.plugin.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<source>${java.version}</source>
<target>${java.version}</target>
<showWeaveInfo>true</showWeaveInfo>
</configuration>
<executions>
<execution>
<id>AspectJ-Classes</id>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>AspectJ-Test-Classes</id>
<phase>process-test-classes</phase>
<goals>
<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>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意: / em>添加程序集插件,使自己更容易测试/验证。
NOTE: Added assembly plugin to make testing/verifying for myself easier.
当我编译项目时,我在maven中看到以下条目:
When I compile the project I see within the maven logs the following entry:
[INFO] --- aspectj-maven-plugin:1.8:compile (AspectJ-Classes) @ testMaven ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] Join point 'method-execution(void testMaven.aaaa.aa(int))' in Type 'testMaven.aaaa' (aaaa.java:5) advised by before advice from 'testMaven.aspecter' (aspecter.java:10)
当我现在执行jar时,我看到如下结果:
When I now execute the jar as follows I see the following result:
...>java -cp target/testMaven-0.0.1-SNAPSHOT-jar-with-dependencies.jar testMaven/MainApp
yooi
2
鉴于MainApp如下:
Given the MainApp is as follows:
package testMaven;
public class MainApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
aaaa a = new aaaa();
a.aa(2);
}
}
启动的类打印传递参数
package testMaven;
public class aaaa {
public void aa(int a){
System.out.println(a);
}
}
拦截器如下
package testMaven;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
@Aspect
public class aspecter {
@Before("execution(* testMaven.aaaa.aa(..))")
public void testBefore(){
System.out.println("yooi");
}
}
我会说它有效:)
这篇关于建议AspectJ一个来自不同Maven项目的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!