1 @annotation()
它表示标注了某个注解的所有方法。
假设有一个接口 Cook,它有两个实现类 CookA、CookB:

Cook:
public interface Cook {
/**
* 制作食品
*/
void make();
}
CookA:
public class CookA implements Cook {
public void make() {
System.out.println("制作食品");
}
}
CookB:
public class CookB implements Cook {
@Log
public void make() {
System.out.println("制作糕点");
}
}
我们在 CookB 的 make() 方法上加了 @Log 注解。
@Log 注解:
@Retention(RetentionPolicy.RUNTIME)//保留期限
@Target(ElementType.METHOD)//目标类型
public @interface Log {
boolean value() default true;//声明成员变量
}
现在使用 @annotation() 来为所有加了 @Log 注解的方法织入增强:
@Aspect
public class AnnotationAspect {
@AfterReturning("@annotation(net.deniro.spring4.anno.Log)")
public void log() {
System.out.println("新增日志");
}
}
单元测试:
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Cook cookA = (Cook) context.getBean("cookA");
cookA.make();
Cook cookB = (Cook) context.getBean("cookB");
cookB.make();
输出结果:
制作食品
制作糕点
新增日志
从输出结果中可以看出,配置了 @Log 注解的方法织入了增强。
2 execution()
它是最常用的切点函数,具体语法如下:
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
假设厨师接口 Cook,存在下面这样的类关系:

CookA 除了会做菜与管理外,还会清洁的技能;CookB 也具有审核的独门技能。
2.1 方法签名定义切点
表达式示例 | 说明 |
---|---|
execution(public * *(..)) |
表示匹配所有目标类的 public 方法 。 第一个 * 代表任意返回类型,第二个 * 代表方法名,而 .. 代表任意入参的方法。 |
execution(* m*(..)) | 表示匹配目标类所有以 m 为前缀的方法。 第一个* 代表任意返回类型,而m* 代表任意以 m 为前缀的方法。这里匹配 make() 与 manage() 方法。 |
2.2 类定义切点
表达式示例 | 说明 |
---|---|
execution(* net.deniro.spring4.aspectj.Cook.*(..)) |
匹配 Cook 接口的所有方法,即这里的 make() 与 manage() 方法。第一个* 代表任意返回类型, net.deniro.spring4.aspectj.Cook.* 表示 Cook 接口的所有方法。 |
execution(* net.deniro.spring4.aspectj.Cook+.*(..)) |
匹配 Cook 接口及其所有实现类的方法,它不但匹配接口中定义的 make() 与 manage() 方法,还匹配它们各自的 clean() 与 audit() 方法。 |
2.3 类包定义切点
类名模式 | 说明 |
---|---|
.* |
包中的所有类,不包括子孙包中的类。 |
..* |
包中以及子孙包的所有类。 |
表达式示例 | 说明 |
---|---|
execution(* net.deniro.*(..)) |
匹配 net.deniro 包中所有类的所有方法。 |
execution(* net.deniro..*(..)) |
匹配 net.deniro 包以及子孙包中所有类的所有方法。 |
2.4 方法入参定义切点
参数中的通配符 | 说明 |
---|---|
* |
表示任意类型参数。 |
.. |
表示任意类型参数且参数不限个数。 |
表达式示例 | 说明 |
---|---|
execution(* make(int,String)) |
这会匹配 make(int,String) 方法。如果方法中的入参类型是 java.lang 包下的类,那么可以直接使用类名;如果是其他类型,那么必须使用全限定名。 |
execution(* make(int,*)) |
匹配 make() 方法,该方法的第一个入参是 int,第二个参数可以是任意类型。 |
execution(* make(int,..)) |
匹配 make() 方法,该方法的第一个入参是 int,后面可以有任意类型的入参且参数个数不限。 |
execution(* make(Object+)) |
匹配 make() 方法,方法只有一个入参,它可以是 Object 类型或该类型的子类。 |
3 args() 与 @args()
3.1 args()
它接收一个类名,表示目前方法的入参对象(运行时)是指定类或子类时,就匹配切点。
表达式示例 | 说明 |
---|---|
args(net.deniro.CookA) |
表示运行时入参类型为 CookA。 |
execution(* *(net.deniro.CookA)) |
表示方法签名中的入参类型为 CookA。 |
3.2 @args()
它接收一个注解类的类名,当方法的运行时入参对象标注了指定注解时,就匹配切点。
假设存在如下继承结构的类关系:

CookB 的方法签名为 method(CookB cook)
,我们称之为入参类型点。标注了@args(Log)
注解的类,我们称之为注解点。
- 如果注解点高于入参类型点,那么入参类型点的类及其子孙类,都不匹配该切点
@args(Log)
:

- 如果注解点低于入参类型点,那么入参类型点的子孙类,都匹配该切点
@args(Log)
:

4 within()
within() 定义的连接点最小范围是类(不是接口),而 execution() 定义的连接点最小范围可以到方法入参,所以 execution() 涵盖了 within() 功能。
within() 语法:within (<类匹配模式>)
表达式示例 | 说明 |
---|---|
within(net.deniro.CookA) |
匹配目标类 CookA 中的所有方法。 |
within(net.deniro.*) |
匹配 net.deniro 包中所有的类,但不包括子孙包中的类。 |
within(net.deniro..*) |
匹配 net.deniro 包及其子孙包中所有的类。 |
5 @within() 与 @target()
这两个注解的入参只能是类名(非接口)。
-
@within(Log)
:匹配标注了@Log
的类及其子孙类。 -
@target(Log)
:匹配标注了@Log
的类。

示例中的 CookB 标注了 @Log
注解,那么 @target(Log)
只匹配 CookB;而 @within(Log)
匹配 CookB 及其子孙类(示例中的 CookC 与 CookD)。
6 target() 与 this()
target() 会根据目标类的类型来确定是否匹配; this() 会根据代理类的类型来确定是否匹配。它们都仅接受类名作为参数。
6.1 target()
target(C) 表示如果目标类的类型是 C时,那么目标类的所有方法都匹配切点。
表达式示例 | 说明 |
---|---|
target(net.deniro.Cook) |
Cook 的所有实现类及其子孙类中的所有方法(包括未在 Cook 接口中定义的方法)都匹配切点。 |
target(net.deniro.Cook+) |
与 target(net.deniro.Cook) 等价。 |
6.2 this()
一般来说, this() 与 target() 是等效的。但在由引介切面产生的代理对象场景中,却表现不同。
我们定义了一个引介切面,让 Cook 拥有 Designer 的技能 O(∩_∩)O哈哈~
Designer 接口:
public interface Designer {
//设计
void design();
}
DefaultDesigner 实现类:
public class DefaultDesigner implements Designer {
public void design() {
System.out.println("开始设计啦");
}
}
DesignerAspect 切面:
@Aspect
public class DesignerAspect {
@DeclareParents(value = "net.deniro.spring4.aspectj.CookA",defaultImpl =
DefaultDesigner.class)
public static Designer designer;
}
接着,再定义了一个使用 this 语法的后置增强切面:
@Aspect
public class ThisAspect {
/**
* 为任何运行期对象为 Designer 类型的 Bean 织入后置增强
*/
@AfterReturning("this(net.deniro.spring4.aspectj.Designer)")
public void test() {
System.out.println("执行 ThisAspect");
}
}
Spring 配置:
<!--aspectj 驱动器 -->
<aop:aspectj-autoproxy/>
<bean id="cookA" class="net.deniro.spring4.aspectj.CookA"/>
<bean class="net.deniro.spring4.aspectj.DesignerAspect"/>
<bean class="net.deniro.spring4.aspectj.ThisAspect"/>
输出结果:
制作食品
执行 ThisAspect
开始设计啦
因为 CookA 被织入了 Designer 接口,所以复合 this 定义的切面,所以 ThisAspect 中的方法被执行啦O(∩_∩)O哈哈~
CookA 通过引介增强了 Designer 接口,那么 this(net.deniro.spring4.aspectj.Designer)
会匹配 CookA 中的所有方法以及 Designer 接口定义的方法。而 target(net.deniro.spring4.aspectj.Designer)
只会匹配Designer 接口定义的方法。
网友评论