Spring AOP借助AspectJ的切点表达式语言来定义Spring切面,下面是切点表达式中使用的指示器:
- execution
这个是主要的切点指示器用来匹配方法执行的连接点,也就是哪些方法要应用切面 - within
用来限定连接点必须在确定的类型或包中 - this
用来限定连接点属于给定类型的一个实例(代理对象) - target
用来限定连接点属于给定类型的一个实例(被代理对象) - args
用来限定连接点,也就是方法执行时它的参数属于给定类型的一个实例 - @target
用来限定连接点属于一个执行对象(被代理对象)所属的拥有给定注解的类 - @args
用来限定连接点,方法执行时传递的参数的运行时类型拥有给定注解 - @within
用来限定连接点属于拥有给定注解的类型中 - @annotation
用来限定连接点的方法拥有给定注解
下面将通过一些测试案例来说明以上各个切点指示器的用法。另外execution不作额外说明,因为比较常见。
package com.drw.start.boot.test;
public interface Fruit {
void print();
}
定义一个水果接口
package com.drw.start.boot.test;
import org.springframework.stereotype.Component;
@Component
public class SweetFruit implements Fruit {
@Override
public void print() {
System.out.println("好吃的甜水果");
}
public void print(int i) {
System.out.println("好吃的甜水果" + i);
}
}
定义一个甜水果类
package com.drw.start.boot.test;
public interface Origin {
void printOrigin();
}
定义一个产地接口
package com.drw.start.boot.test;
public interface FruitWeight {
public int getWeight();
}
定义一个水果重量接口
package com.drw.start.boot.test;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize
public class AppleWeight implements FruitWeight {
public int weight;
public AppleWeight(int weight) {
this.weight = weight;
}
@Override
public int getWeight() {
return weight;
}
}
定义一个苹果重量类
package com.drw.start.boot.test;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Component
@Validated
public class RedApple extends SweetFruit implements Origin {
@Override
public void print() {
System.out.println("好吃的红苹果");
}
@Override
public void printOrigin() {
System.out.println("浙江产的红苹果");
}
public void printPrice() {
System.out.println("红苹果10块一斤");
}
public void printPrice(Integer price) {
System.out.println("红苹果" + price + "块一斤");
}
public void printPrice(Integer price, String mesg) {
System.out.println("红苹果" + price + "块一斤");
}
public void printWeight(FruitWeight weight) {
System.out.println("这个苹果" + weight.getWeight() + "克");
}
}
定义一个红苹果类
package com.drw.start.boot.test;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Component
public class FruitFarmer {
@JsonSerialize
public void print() {
System.out.println("种水果的农民");
}
@JsonSerialize
public void worker() {
System.out.println("果农在劳动");
}
}
定义一个果农类
package com.drw.start.boot.test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AopDesignatorTest {
@Autowired
@Qualifier("sweetFruit")
private SweetFruit sweetFruit;
@Autowired
@Qualifier("redApple")
private RedApple redApple;
@Autowired
@Qualifier("redApple")
private Fruit rFruit;
@Autowired
@Qualifier("sweetFruit")
private Fruit sFruit;
@Autowired
private FruitFarmer fruitFarmer;
@org.junit.Test
public void test() {
}
}
使用的测试类
SweetFruit实现Fruit接口。RedApple继承SweetFruit并且实现Origin接口,并且关联FruitWeight接口。AppleWeight实现FruitWeight接口。
within
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("within(com.drw.start.boot.test.Fruit)")
public void withinTest(JoinPoint point) {
System.out.println("within(com.drw.start.boot.test.Fruit) - " + point.getSignature());
}
@Before("within(com.drw.start.boot.test.SweetFruit)")
public void withinTest1(JoinPoint point) {
System.out.println("within(com.drw.start.boot.test.SweetFruit) - " + point.getSignature());
}
@Before("within(com.drw.start.boot.test.RedApple)")
public void withinTest2(JoinPoint point) {
System.out.println("within(com.drw.start.boot.test.RedApple) - " + point.getSignature());
}
}
定义包含within指示器的切面
@org.junit.Test
public void test() {
System.out.println("-------------Fruit(SweetFruit)-----------");
sFruit.print();
System.out.println("-------------Fruit(RedApple)-------------");
rFruit.print();
System.out.println("-------------SweetFruit------------------");
sweetFruit.print();
System.out.println("-------------RedApple--------------------");
redApple.print();
redApple.printOrigin();
System.out.println("-------------FruitFarmer-----------------");
fruitFarmer.print();
}
测试用例
-------------Fruit(SweetFruit)-----------
within(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.SweetFruit.print()
好吃的甜水果
-------------Fruit(RedApple)-------------
within(com.drw.start.boot.test.RedApple) - void com.drw.start.boot.test.RedApple.print()
好吃的红苹果
-------------SweetFruit------------------
within(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.SweetFruit.print()
好吃的甜水果
-------------RedApple--------------------
within(com.drw.start.boot.test.RedApple) - void com.drw.start.boot.test.RedApple.print()
好吃的红苹果
within(com.drw.start.boot.test.RedApple) - void com.drw.start.boot.test.RedApple.printOrigin()
浙江产的红苹果
-------------FruitFarmer-----------------
种水果的农民
within指示器用来限定连接点必须在确定的类型或包中,在上面的切面中定义了Fruit、SweetFruit、RedApple这三个类型,根据输出发现只有SweetFruit、RedApple这两个切面进行了拦截。这说明了within指示器只拦截确定的类型,也就是跟它的接口无关,定义什么类型就拦截什么类型。
this
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("this(com.drw.start.boot.test.Fruit)")
public void thisTest(JoinPoint point) {
System.out.println("this(com.drw.start.boot.test.Fruit) - " + point.getSignature());
}
@Before("this(com.drw.start.boot.test.SweetFruit)")
public void thisTest1(JoinPoint point) {
System.out.println("this(com.drw.start.boot.test.SweetFruit) - " + point.getSignature());
}
@Before("this(com.drw.start.boot.test.RedApple)")
public void thisTest2(JoinPoint point) {
System.out.println("this(com.drw.start.boot.test.RedApple) - " + point.getSignature());
}
@Before("this(com.drw.start.boot.test.Origin)")
public void thisTest3(JoinPoint point) {
System.out.println("this(com.drw.start.boot.test.Origin) - " + point.getSignature());
}
}
定义包含this指示器的切面
@org.junit.Test
public void testThis() {
System.out.println("-------------SweetFruit------------------");
sweetFruit.print();
System.out.println("-------------RedApple--------------------");
redApple.print();
redApple.printOrigin();
System.out.println("-------------FruitFarmer------------------");
fruitFarmer.print();
}
测试用例
-------------SweetFruit------------------
this(com.drw.start.boot.test.Fruit) - void com.drw.start.boot.test.SweetFruit.print()
this(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.SweetFruit.print()
好吃的甜水果
-------------RedApple--------------------
this(com.drw.start.boot.test.Fruit) - void com.drw.start.boot.test.RedApple.print()
this(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.RedApple.print()
this(com.drw.start.boot.test.RedApple) - void com.drw.start.boot.test.RedApple.print()
this(com.drw.start.boot.test.Origin) - void com.drw.start.boot.test.RedApple.print()
好吃的红苹果
this(com.drw.start.boot.test.Fruit) - void com.drw.start.boot.test.RedApple.printOrigin()
this(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.RedApple.printOrigin()
this(com.drw.start.boot.test.RedApple) - void com.drw.start.boot.test.RedApple.printOrigin()
this(com.drw.start.boot.test.Origin) - void com.drw.start.boot.test.RedApple.printOrigin()
浙江产的红苹果
-------------FruitFarmer------------------
种水果的农民
this指示器用来限定连接点属于给定类型的一个实例(代理对象),从上面的输出中可以看到,sweetFruit.print();被this(...Fruit)、this(...SweetFruit)拦截,redApple.print();和redApple.printOrigin();被this(...Fruit)、this(...SweetFruit)、this(...RedApple)、this(...Origin)拦截。这说明了不管方法是来源于哪个接口或类(redApple.printOrigin();来源于Origin接口),只要代理对象的实例属于this中所定义的类型,那么这个方法就会被拦截。比如sweetFruit的代理对象既是Fruit的一个实例,也是SweetFruit的一个实例。redApple的代理对象分别属于Fruit、SweetFruit、RedApple、Origin这四个类型的一个实例。
注:我这边的测试环境显示AOP使用了CGLIB代理,也就是继承代理,所以代理对象同属以上接口或类,如果使用了JDK动态代理可能会产生不同的结果
target
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("target(com.drw.start.boot.test.Fruit)")
public void targetTest(JoinPoint point) {
System.out.println("target(com.drw.start.boot.test.Fruit) - " + point.getSignature());
}
@Before("target(com.drw.start.boot.test.SweetFruit)")
public void targetTest1(JoinPoint point) {
System.out.println("target(com.drw.start.boot.test.SweetFruit) - " + point.getSignature());
}
@Before("target(com.drw.start.boot.test.RedApple)")
public void targetTest2(JoinPoint point) {
System.out.println("target(com.drw.start.boot.test.RedApple) - " + point.getSignature());
}
@Before("target(com.drw.start.boot.test.Origin)")
public void targetTest3(JoinPoint point) {
System.out.println("target(com.drw.start.boot.test.Origin) - " + point.getSignature());
}
}
定义包含target指示器的切面
@org.junit.Test
public void testTarget() {
System.out.println("-------------SweetFruit------------------");
sweetFruit.print();
System.out.println("-------------RedApple--------------------");
redApple.print();
redApple.printOrigin();
System.out.println("-------------FruitFarmer------------------");
fruitFarmer.print();
}
测试用例
-------------SweetFruit------------------
target(com.drw.start.boot.test.Fruit) - void com.drw.start.boot.test.SweetFruit.print()
target(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.SweetFruit.print()
好吃的甜水果
-------------RedApple--------------------
target(com.drw.start.boot.test.Fruit) - void com.drw.start.boot.test.RedApple.print()
target(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.RedApple.print()
target(com.drw.start.boot.test.RedApple) - void com.drw.start.boot.test.RedApple.print()
target(com.drw.start.boot.test.Origin) - void com.drw.start.boot.test.RedApple.print()
好吃的红苹果
target(com.drw.start.boot.test.Fruit) - void com.drw.start.boot.test.RedApple.printOrigin()
target(com.drw.start.boot.test.SweetFruit) - void com.drw.start.boot.test.RedApple.printOrigin()
target(com.drw.start.boot.test.RedApple) - void com.drw.start.boot.test.RedApple.printOrigin()
target(com.drw.start.boot.test.Origin) - void com.drw.start.boot.test.RedApple.printOrigin()
浙江产的红苹果
-------------FruitFarmer------------------
种水果的农民
target指示器用来限定连接点属于给定类型的一个实例(被代理对象),这个指示器的语义跟this指示器是很相似的,然后从上面的输出来看它们也是一样的(除了指示器不同)。但是它们有一个重要的区别,this中的实例指的是代理对象,而target中的实例指的是被代理对象。
args
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("within(com.drw.start.boot.test.*) && args(java.lang.Integer, java.lang.String)")
public void argsTest(JoinPoint point) {
System.out.println("args(java.lang.Integer, java.lang.String) - " + point.getSignature());
}
}
定义包含args指示器的切面,上面的切面表达式中额外定义了within指示器,这个主要是为了缩小args的使用范围。如果不加,Spring AOP会尝试去代理所有符合条件的对象,但是有些对象的访问会有限制,导致启动异常。这个也提醒我们使用AOP时必须要明确指定使用范围,否则会造成不可预料的错误。
@org.junit.Test
public void testArgs() {
System.out.println("-------------RedApple--------------------");
redApple.printPrice();
redApple.printPrice(10);
redApple.printPrice(1000, "这个苹果价格很贵");
}
测试用例,回顾一下之前定义的三个printPrice方法:
public void printPrice() {
System.out.println("红苹果10块一斤");
}
public void printPrice(Integer price) {
System.out.println("红苹果" + price + "块一斤");
}
public void printPrice(Integer price, String mesg) {
System.out.println("红苹果" + price + "块一斤");
}
-------------RedApple--------------------
红苹果10块一斤
红苹果15块一斤
args(java.lang.Integer, java.lang.String) - void com.drw.start.boot.test.RedApple.printPrice(Integer,String)
红苹果1000块一斤
args指示器用来限定连接点,也就是方法执行时它的参数属于给定类型的一个实例,从上面的输出来看,只有public void printPrice(Integer price, String mesg)这个方法被拦截,因为只有它符合条件。如果我们将定义改为public void printPrice(String mesg,Integer price ),结果如下:
-------------RedApple--------------------
红苹果10块一斤
红苹果15块一斤
红苹果1000块一斤
也就是说args指示器不但对参数类型有要求,而且还会对参数个数、定义顺序有要求。
@target
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("within(com.drw.start.boot.test.*) && @target(org.springframework.validation.annotation.Validated)")
public void _targetTest(JoinPoint point) {
System.out.println("@target(org.springframework.validation.annotation.Validated) - " + point.getSignature());
}
}
定义包含@target指示器的切面
@org.junit.Test
public void test_target() {
System.out.println("-------------RedApple--------------------");
redApple.print();
System.out.println("-------------FruitFarmer------------------");
fruitFarmer.print();
}
测试用例,然后回顾一下之前定义的RedApple这个类上所注解的@Validated:
@Component
@Validated
public class RedApple extends SweetFruit implements Origin {
...
}
-------------RedApple--------------------
@target(org.springframework.validation.annotation.Validated) - void com.drw.start.boot.test.RedApple.print()
好吃的红苹果
-------------FruitFarmer------------------
种水果的农民
现在删除RedApple上的@Validated注解,将这个注解放到SweetFruit上,输出如下:
-------------RedApple--------------------
好吃的红苹果
-------------FruitFarmer------------------
种水果的农民
重复以上步骤,将@Validated注解放到Fruit接口上,输出如下:
-------------RedApple--------------------
好吃的红苹果
-------------FruitFarmer------------------
种水果的农民
@target用来限定连接点属于一个执行对象(被代理对象)所属的拥有给定注解的类。虽然@target和target名称相似,但是它们用法是完全不同的,前者针对注解,后置针对类型。另外从上面的输出可以看出@target限定在一个执行对象的所属类,与它的父类接口无关。
@within
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("within(com.drw.start.boot.test.*) && @within(org.springframework.validation.annotation.Validated)")
public void _withinTest(JoinPoint point) {
System.out.println("@within(org.springframework.validation.annotation.Validated) - " + point.getSignature());
}
}
定义包含@within指示器的切面
@org.junit.Test
public void test_within() {
System.out.println("-------------RedApple--------------------");
redApple.print();
redApple.print(11);
System.out.println("-------------FruitFarmer------------------");
fruitFarmer.print();
}
测试用例,然后注意redApple.print(11);它是定义在SweetFruit类中
-------------RedApple--------------------
@within(org.springframework.validation.annotation.Validated) - void com.drw.start.boot.test.RedApple.print()
好吃的红苹果
好吃的甜水果11
-------------FruitFarmer------------------
种水果的农民
现在删除RedApple上的@Validated注解,将这个注解放到SweetFruit上,输出如下:
-------------RedApple--------------------
好吃的红苹果
@within(org.springframework.validation.annotation.Validated) - void com.drw.start.boot.test.SweetFruit.print(int)
好吃的甜水果11
-------------FruitFarmer------------------
种水果的农民
重复以上步骤,将@Validated注解放到Fruit接口上,输出如下:
-------------RedApple--------------------
好吃的红苹果
好吃的甜水果11
-------------FruitFarmer------------------
种水果的农民
@within用来限定连接点属于拥有给定注解的类型中。从上面的输出可以看出,@target针对执行对象所属的类,而@within针对执行对象所属的类型。另外所执行的方法必须属于拥有给定注解的类型中,比如redApple.print();被重写在RedApple类中,redApple.print(11);被定义在SweetFruit类中,当这两个类拥有指定注解后方法执行时才会被拦截。最后需要注意的是@within和within只是名称接近,实际使用效果是不同的。
@args
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("within(com.drw.start.boot.test.*) && @args(com.fasterxml.jackson.databind.annotation.JsonDeserialize)")
public void _argsTest(JoinPoint point) {
System.out.println("@args(com.fasterxml.jackson.databind.annotation.JsonDeserialize) - " + point.getSignature());
}
}
定义包含@args指示器的切面
@org.junit.Test
public void test_args() {
System.out.println("-------------RedApple--------------------");
redApple.printPrice(100);
FruitWeight fruitWeight = new AppleWeight(500);
redApple.printWeight(fruitWeight);
}
测试用例,回顾一下在RedApple中定义的方法以及FruitWeight接口的定义:
...
public void printPrice(Integer price) {
System.out.println("红苹果" + price + "块一斤");
}
...
****************************************************
package com.drw.start.boot.test;
public interface FruitWeight {
public int getWeight();
}
****************************************************
package com.drw.start.boot.test;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize
public class AppleWeight implements FruitWeight {
public int weight;
public AppleWeight(int weight) {
this.weight = weight;
}
@Override
public int getWeight() {
return weight;
}
}
-------------RedApple--------------------
红苹果100块一斤
@args(com.fasterxml.jackson.databind.annotation.JsonDeserialize) - void com.drw.start.boot.test.RedApple.printWeight(FruitWeight)
这个苹果500克
现在删除AppleWeight上的@JsonDeserialize注解,将这个注解放到FruitWeight上,输出如下:
-------------RedApple--------------------
红苹果100块一斤
@args(com.fasterxml.jackson.databind.annotation.JsonDeserialize) - void com.drw.start.boot.test.RedApple.printWeight(FruitWeight)
这个苹果500克
现在将@JsonDeserialize注解放到AppleWeight上,并且再定义一个继承了AppleWeight的子类:
package com.drw.start.boot.test;
public class RedAppleWeight extends AppleWeight {
public RedAppleWeight(int weight) {
super(weight);
}
}
@org.junit.Test
public void test_args() {
System.out.println("-------------RedApple--------------------");
redApple.printPrice(100);
FruitWeight fruitWeight = new AppleWeight(500);
FruitWeight redAppleWeight = new RedAppleWeight(200);
redApple.printWeight(fruitWeight);
redApple.printWeight(redAppleWeight);
}
测试用例
-------------RedApple--------------------
红苹果100块一斤
@args(com.fasterxml.jackson.databind.annotation.JsonDeserialize) - void com.drw.start.boot.test.RedApple.printWeight(FruitWeight)
这个苹果500克
这个苹果200克
@args用来限定连接点,方法执行时传递的参数的运行时类型拥有给定注解。根据以上的输出以及@args的定义,这个给定的注解要么定义在方法参数的类型本身上,那么定义在的它的实现类上。比如RedAppleWeight它虽然继承了AppleWeight(拥有指定注解),但是它本身没有指定注解,并且当FruitWeight也没有指定注解时,相关方法不会被拦截。
@annotation
package com.drw.start.boot.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AopDesignator {
@Before("within(com.drw.start.boot.test.*) && @annotation(com.fasterxml.jackson.databind.annotation.JsonSerialize)")
public void _annotationTest(JoinPoint point) {
System.out.println("@annotation(com.fasterxml.jackson.databind.annotation.JsonSerialize) - " + point.getSignature());
}
}
定义包含@annotation指示器的切面
@org.junit.Test
public void test_annotation() {
System.out.println("-------------FruitFarmer------------------");
fruitFarmer.print();
fruitFarmer.worker();
}
测试用例,回顾一下FruitFarmer的定义:
package com.drw.start.boot.test;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Component
public class FruitFarmer {
@JsonSerialize
public void print() {
System.out.println("种水果的农民");
}
@JsonSerialize
public void worker() {
System.out.println("果农在劳动");
}
}
-------------FruitFarmer------------------
@annotation(com.fasterxml.jackson.databind.annotation.JsonSerialize) - void com.drw.start.boot.test.FruitFarmer.print()
种水果的农民
@annotation(com.fasterxml.jackson.databind.annotation.JsonSerialize) - void com.drw.start.boot.test.FruitFarmer.worker()
果农在劳动
现在删除print()上的@JsonSerialize注解,输出如下:
-------------FruitFarmer------------------
种水果的农民
@annotation(com.fasterxml.jackson.databind.annotation.JsonSerialize) - void com.drw.start.boot.test.FruitFarmer.worker()
果农在劳动
@annotation用来限定连接点的方法拥有给定注解。这个指示器比较容易理解,就是目标方法上拥有给定注解就可以了。
参考资源








网友评论