我有以下 AspectJ 示例,我已经将其作为“hello world”风格的概念证明。StyleAspect尽管实际代码SomeClass只执行一次(根据需要),但 中的建议代码似乎执行了两次。
这是代码:
首先是一个名为 WithStyle 的注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface WithStyle {
}
Run Code Online (Sandbox Code Playgroud)
然后,拦截任何带有@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;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,一些带有注释的代码
public class SomeClass {
@WithStyle
public void doIt() {
System.out.println("I'm doing it....");
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下输出:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ AspectJTest ---
Doing it in style...
Doing it in style...
I'm doing it....
Done
Done
Run Code Online (Sandbox Code Playgroud)
所以看起来好像虽然代码本身只执行一次,但方面中的代码却被执行了两次。
这是调用代码:
public class Main {
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
someClass.doIt();
}
}
Run Code Online (Sandbox Code Playgroud)
为了完整起见,我在 AspectJ 插件配置中包含了 pom
<?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>
Run Code Online (Sandbox Code Playgroud)
您的around() 建议是拦截用( ie, )注释的方法的thecall和executionjoin 点。如果你在你的方面添加一个:@WithStyle 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;
}
}
Run Code Online (Sandbox Code Playgroud)
你会得到以下输出:
call(public void SomeClass.doIt()) <----
Doing it in style...
execution(public void SomeClass.doIt()) <----
Doing it in style...
I'm doing it....
Done
Done
Run Code Online (Sandbox Code Playgroud)
您可以清楚地看到连接点call和execution方法SomeClass.doIt()正在被around 通知 拦截doItWithStyle。
从截取的 来看call,around 忠告是编排代码如下:
// around advice code before the pjp.proceed();
someClass.doIt(); // during the pjp.proceed();
// around advice code after the pjp.proceed();
Run Code Online (Sandbox Code Playgroud)
最后:
System.out.println("Doing it in style...");.
someClass.doIt();
System.out.println("Done");
Run Code Online (Sandbox Code Playgroud)
从执行:
@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();
}
Run Code Online (Sandbox Code Playgroud)
最后:
@WithStyle
public void doIt() {
System.out.println("Doing it in style...");
System.out.println("I'm doing it....");
System.out.println("Done");
}
Run Code Online (Sandbox Code Playgroud)
导致输出:
Doing it in style...
Doing it in style...
I'm doing it....
Done
Done
Run Code Online (Sandbox Code Playgroud)
现在,如果您想避免around 通知拦截方法的thecall和 the 。您需要进一步限制您的通知拦截的连接点。要拦截该方法,您可以执行以下操作:executiondoIt()around call
@Around("@annotation(WithStyle) && call(* *(..))")
Run Code Online (Sandbox Code Playgroud)
对于方法execution:
@Around("@annotation(WithStyle) && execution(* *(..))")
Run Code Online (Sandbox Code Playgroud)
您可以通过调整call或execution切入点的签名,根据方法的参数数量、其返回类型、名称等进一步限制被拦截的连接点。
| 归档时间: |
|
| 查看次数: |
223 次 |
| 最近记录: |