Java 为什么AspectJ@Around建议执行两次?
我有下面的AspectJ示例,作为“hello world”风格的概念证明。Java 为什么AspectJ@Around建议执行两次?,java,aop,aspectj,Java,Aop,Aspectj,我有下面的AspectJ示例,作为“hello world”风格的概念证明。StyleAspect中的建议代码似乎执行了两次,即使SomeClass中的实际代码只执行了一次(根据需要) 代码如下: 首先,一个名为WithStyle的注释: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface WithStyle { } 然后,一个截取带有@WithStyle注释的任何代码的方面 @
StyleAspect
中的建议代码似乎执行了两次,即使SomeClass
中的实际代码只执行了一次(根据需要)
代码如下:
首先,一个名为WithStyle的注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface WithStyle {
}
然后,一个截取带有@WithStyle注释的任何代码的方面
@Aspect
public class StyleAspect {
@Around("@annotation(WithStyle)")
public Object doItWithStyle(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Doing it in style...");
Object result = pjp.proceed();
System.out.println("Done");
return result;
}
}
public class SomeClass {
@WithStyle
public void doIt() {
System.out.println("I'm doing it....");
}
}
最后是一些带有注释的代码
@Aspect
public class StyleAspect {
@Around("@annotation(WithStyle)")
public Object doItWithStyle(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Doing it in style...");
Object result = pjp.proceed();
System.out.println("Done");
return result;
}
}
public class SomeClass {
@WithStyle
public void doIt() {
System.out.println("I'm doing it....");
}
}
运行此操作时,我得到以下输出:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ AspectJTest ---
Doing it in style...
Doing it in style...
I'm doing it....
Done
Done
call(public void SomeClass.doIt()) <----
Doing it in style...
execution(public void SomeClass.doIt()) <----
Doing it in style...
I'm doing it....
Done
Done
因此,似乎代码本身只执行一次,而方面中的代码执行了两次
以下是呼叫代码:
public class Main {
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
someClass.doIt();
}
}
为了完整性,我将pom与AspectJ插件配置一起包含
<?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>ie.philb</groupId>
<artifactId>AspectJTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.java.target>1.8</project.build.java.target>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4.0.0
菲利布
AspectJTest
1.0-快照
罐子
UTF-8
1.8
1.8
1.8
org.aspectj
aspectjrt
1.9.6
org.aspectj
aspectjweaver
1.9.6
org.codehaus.mojo
aspectj maven插件
1.11
1.8
1.8
1.8
编撰
测试编译
您的around()
建议正在拦截调用
和执行
方法的连接点(即doIt()
)。如果添加System.out.println(pjp)代码>到您的方面:
@Aspect
public class StyleAspect {
@Around("@annotation(WithStyle) ")
public Object doItWithStyle(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(pjp);
System.out.println("Doing it in style...");
Object result;
try{
result = pjp.proceed();
}
finally{
System.out.println("Done");
}
return result;
}
}
您将获得以下输出:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ AspectJTest ---
Doing it in style...
Doing it in style...
I'm doing it....
Done
Done
call(public void SomeClass.doIt()) <----
Doing it in style...
execution(public void SomeClass.doIt()) <----
Doing it in style...
I'm doing it....
Done
Done
因此:
System.out.println("Doing it in style...");.
someClass.doIt();
System.out.println("Done");
@WithStyle
public void doIt() {
System.out.println("Doing it in style...");
System.out.println("I'm doing it....");
System.out.println("Done");
}
从执行开始:
@WithStyle
public void doIt() {
// around advice code before the pjp.proceed();
System.out.println("I'm doing it....");
// around advice code after the pjp.proceed();
}
因此:
System.out.println("Doing it in style...");.
someClass.doIt();
System.out.println("Done");
@WithStyle
public void doIt() {
System.out.println("Doing it in style...");
System.out.println("I'm doing it....");
System.out.println("Done");
}
导致输出:
Doing it in style...
Doing it in style...
I'm doing it....
Done
Done
现在,如果您想避免周围的建议同时拦截调用和执行方法doIt()
。您需要进一步限制您的建议拦截的连接点。要仅截取方法调用
,您可以执行以下操作:
@Around("@annotation(WithStyle) && call(* *(..))")
对于方法执行
:
@Around("@annotation(WithStyle) && execution(* *(..))")
通过调整调用
或执行
切入点的签名,可以根据方法的参数数量、返回类型、名称等进一步限制被拦截的连接点