好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

谈谈Spring AOP中@Aspect的高级用法示例

前言

本文主要跟大家分享介绍了关于spring aop中@aspect的高级用法,下面话不多说了,来随着小编一起看看详细的介绍吧。

1 切点复合运算

支持在切点定义中加入以下运算符进行复合运算:

 

运算符 说明
&& 与运算。
! 非运算。
|| 或运算。

 

2 切点命名

一般情况下,切点是直接声明在需要增强方法处,这种切点的声明方式称为匿名切点,匿名切点只能在声明处被使用 。 如果希望在其它地方可以重用这个切点,我们可以通过 @pointcut 注解及切面类方法来命名它。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

public class namepointcut {

 

  /**

  * 切点被命名为 method1,且该切点只能在本类中使用

  */

  @pointcut ( "within(net.deniro.spring4.aspectj.*)" )

  private void method1() {

  }

 

  /**

  * 切点被命名为 method2,且该切点可以在本类或子孙类中使用

  */

  @pointcut ( "within(net.deniro.spring4.aspectj.*)" )

  protected void method2() {

  }

 

  /**

  * 切点被命名为 method3,且该切点可以在任何类中使用

  * 这里还使用了复合运算

  */

  @pointcut ( "method1() && method2()" )

  public void method3() {

  }

}

命名切点的结构如下:

切点可访问性修饰符与类可访问性修饰符的功能是相同的,它可以决定定义的切点可以在哪些类中可使用。

因为命名切点仅利用方法名及访问修饰符的信息,所以我们一般定义方法的返回类型为 void ,并且方法体为空 。

定义好切点后,就可以在切面类中引用啦:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@aspect

public class namepointcutaspect {

 

  @after ( "namepointcut.method2()" )

  public void aspectmethod1() {

  }

 

  /**

  * 这里使用了复合运算

  */

  @after ( "namepointcut.method2() && namepointcut.method3()" )

  public void aspectmethod2() {

  }

}

3 织入顺序

一个连接点可以同时匹配多个切点,而切点所对应的增强在连接点上织入顺序的规则是这样的:

1.如果在同一个切面类中声明的增强,则按照增强在切面类中定义的顺序进行织入;

2.如果增强位于不同的切面类中,并且这些切面类都实现了org.springframework.core.ordered 接口,则由 ordered 方法的顺序号决定(顺序号小的先织入);

3.如果增强位于不同的切面类中,但这些切面类没有实现org.springframework.core.ordered 接口,织入的顺序是不确定的 。

假设有两个切面类 a 与 b,它们都实现了 ordered 接口,a 的顺序号为 1,b 的顺序号为 2,切面类 a 与 b 都定义了 3 个增强,那么同时匹配这 6 个增强的织入顺序如下图所示:

4 获取连接点信息

4.1 joinpoint

org.aspectj.lang.joinpoint 接口表示目标类连接点对象,它定义这些主要方法。

 

方法 说明
object[] getargs() 获取连接点方法运行时的入参列表。
signature getsignature() 获取连接点的方法签名对象。
object gettarget() 获取连接点所在的目标对象。
object getthis() 获取代理对象。

 

4.2 proceedingjoinpoint

org.aspectj.lang.proceedingjoinpoint 继承了 joinpoint 接口,它新增了两个方法(它们用于执行连接点方法)。

方法 说明
object proceed() throws throwable 通过反射执行目标对象连接点处的方法。
object proceed(object[] var1) throws throwable 使用新的入参(替换掉原来的入参),通过反射执行目标对象连接点处的方法。

4.3 示例

cook 接口:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public interface cook {

 

  /**

  * 制作食品

  */

  void make();

 

  /**

  * 制作

  *

  * @param name 食品名称

  */

  void make(string name);

}

cooka 类:

?

1

2

3

4

5

6

7

8

9

public class cooka implements cook {

  public void make() {

  system.out.println( "制作食品" );

  }

 

  public void make(string name) {

  system.out.println( "制作" + name);

  }

}

在切面类中访问连接点信息:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@aspect

public class joinpointaspect {

 

  @around ( "within(net.deniro.spring4.aspectj.cooka)" )

  public void test(proceedingjoinpoint pjp) throws throwable {

  system.out.println( "---------获取连接点对象【开始】---------" );

  system.out.println( "参数:" + pjp.getargs()[ 0 ]);

  system.out.println( "签名对象:" + pjp.gettarget().getclass());

 

  //执行目标对象方法

  pjp.proceed();

  system.out.println( "---------获取连接点对象【结束】---------" );

 

  }

}

spring bean 配置:

?

1

2

3

4

5

6

7

8

9

10

11

12

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://www.springframework.org/schema/beans"

  xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"

  xmlns:aop= "http://www.springframework.org/schema/aop"

  xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >

 

  <!--aspectj 驱动器 -->

  <aop:aspectj-autoproxy/>

 

  <bean id= "cooka" class = "net.deniro.spring4.aspectj.cooka" />

  <bean class = "net.deniro.spring4.aspectj.joinpointaspect" />

</beans>

输出结果:

---------获取连接点对象【开始】---------
参数:寿司
签名对象:class net.deniro.spring4.aspectj.cooka
制作寿司
---------获取连接点对象【结束】---------

5 绑定连接点的方法入参

args()、this()、target()、@args()、@within()、@target() 和 @annotation() 这些切点函数除可以指定类名外,还可以指定参数名,将目标对象连接点上的方法入参绑定到增强的方法中 。 其中 args() 用于绑定连接点方法的入参, @annotation() 用于绑定连接点方法的注解对象,而 @args() 用于绑定连接点方法入参的注解。

cookc 类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public class cookc implements cook {

  public void make() {

  system.out.println( "制作食品" );

  }

 

  public void make(string name) {

  system.out.println( "制作" + name);

  }

 

  public void make(string name, int num) {

  system.out.println( "制作" + name + " " + num + " 个" );

  }

}

切面类:

?

1

2

3

4

5

6

7

8

9

10

11

12

@aspect

public class paramsaspect {

 

  @before ( "target(net.deniro.spring4.aspectj.cookc) && args(name,num,..)" )

  public void test(string name, int num) {

  system.out.println( "----------绑定连接点入参【开始】----------" );

  system.out.println( "name:" + name);

  system.out.println( "num:" + num);

  system.out.println( "----------绑定连接点入参【结束】----------" );

 

  }

}

这里的连接点表达式 args(name,num,..) 会先找到 name 与 num 的类型,从而生成真正的表达式 args(string,int,..)。 增强方法可以通过 name 与 num 得到连接点的方法入参。

切点匹配和参数绑定的过程是这样的:

args()会根据参数名称在增强方法中查到名称相同的入参并获得对应参数的类型,这样就得到了匹配连接点方法的入参类型 。 连接点方法入参类型所在的位置由参数名在 args() 函数中声明的位置决定 。

上述示例中的匹配过程如下:

spring 配置:

?

1

2

3

4

5

<!--aspectj 驱动器 -->

<aop:aspectj-autoproxy proxy-target- class = "true" />

 

<bean id= "cookc" class = "net.deniro.spring4.aspectj.cookc" />

<bean class = "net.deniro.spring4.aspectj.paramsaspect" />

注意: 这里必须通过 <aop:aspectj-autoproxy proxy-target-class="true" />来启用 cglib 动态代理,这是因为 cookc 的 public void make(string name, int num) 是该类独有的方法(非接口定义的方法),所以必须使用 cglib 生成子类的代理方法 。

单元测试:

?

1

2

3

applicationcontext context = new classpathxmlapplicationcontext( "spring-beans.xml" );

cookc cookc = (cookc) context.getbean( "cookc" );

cookc.make( "寿司" , 100 );

输出结果:

----------绑定连接点入参【开始】----------
name:寿司
num:100
----------绑定连接点入参【结束】----------
制作寿司 100 个

6 绑定代理对象

使用 this() 或 target() 可绑定被代理对象的实例。通过类实例名绑定对象时,依然具有原来连接点匹配的功能,只是类名是由增强方法中的同名入参类型间接决定的。

?

1

2

3

4

5

6

7

8

9

10

@aspect

public class proxyaspect {

 

  @before ( "this(cook)" )

  public void bind(cook cook) {

   system.out.println( "--------绑定代理对象【开始】--------" );

   system.out.println(cook.getclass().getname());

   system.out.println( "--------绑定代理对象【结束】--------" );

  }

}

首先通过 public void bind(cook cook) 找出 cook 所对应的类型,接着转换切点表达式为 this(net.deniro.spring4.aspectj.cook) 。这样就表示该切点匹配所有代理对象为 cook 类中的所有方法。

输出结果:

--------绑定代理对象【开始】--------
net.deniro.spring4.aspectj.cookc$$enhancerbyspringcglib$$217fb793
--------绑定代理对象【结束】--------

target() 绑定与 this() 相似。

7 绑定类注解对象

@within() 和 @target() 函数都可以将目标类的注解对象绑定到增强方法中。

定义一个日志注解类:

?

1

2

3

4

5

@retention (retentionpolicy.runtime) //保留期限

@target ({elementtype.method,elementtype.type}) //目标类型

public @interface log {

  boolean value() default true ; //声明成员变量

}

把该注解类应用于 cookd:

?

1

2

3

4

5

6

7

8

9

10

@log

public class cookd implements cook {

  public void make() {

   system.out.println( "制作糕点" );

  }

 

  public void make(string name) {

 

  }

}

绑定类注解对象:

?

1

2

3

4

5

6

7

8

9

10

@aspect

public class classannotationobjectaspect {

 

  @before ( "@within(log)" )

  public void bind(log log){

   system.out.println( "----------绑定类注解对象【开始】----------" );

   system.out.println(log.getclass().getname());

   system.out.println( "----------绑定类注解对象【结束】----------" );

  }

}

spring 配置:

?

1

2

3

4

5

6

<!--aspectj 驱动器 -->

<aop:aspectj-autoproxy proxy-target- class = "true" />

 

 

<bean id= "cookd" class = "net.deniro.spring4.aspectj.cookd" />

<bean class = "net.deniro.spring4.aspectj.classannotationobjectaspect" />

单元测试:

?

1

2

3

applicationcontext context = new classpathxmlapplicationcontext( "spring-beans.xml" );

cookd cook = (cookd) context.getbean( "cookd" );

cook.make();

输出结果:

----------绑定类注解对象【开始】----------
com.sun.proxy.$proxy8
----------绑定类注解对象【结束】----------

从输出结果  com.sun.proxy.$proxy8 可以看出 ,cookd 类的注解 log 对象也被代理咯o(∩_∩)o哈哈~

8 绑定返回值

在后置增强中,可以通过 returning 来绑定连接点方法的返回值。

切面:

?

1

2

3

4

5

6

7

8

9

10

@aspect

public class returnvalueaspect {

 

  @afterreturning (value = "target(net.deniro.spring4.aspectj.cooka)" , returning = "value" )

  public void bind( boolean value) {

   system.out.println( "绑定返回值【开始】" );

   system.out.println( "value:" + value);

   system.out.println( "绑定返回值【结束】" );

  }

}

注意: returning 的值必须与方法参数名相同。

cooka 新增 smell 方法:

?

1

2

3

4

public boolean smell(string name) {

  system.out.println(name + "香吗?" );

  return true ;

}

单元测试:

?

1

2

3

applicationcontext context = new classpathxmlapplicationcontext( "spring-beans.xml" );

cooka cook = (cooka) context.getbean( "cooka" );

cook.smell( "烤鸭" );

输出结果:

烤鸭香吗?
绑定返回值【开始】
value:true
绑定返回值【结束】

9 绑定异常

可以使用 afterthrowing 注解的 throwing 成员变量来绑定连接点抛出的异常。

切面类:

?

1

2

3

4

5

6

7

8

9

10

@aspect

public class exceptionaspect {

 

  @afterthrowing (value = "target(net.deniro.spring4.aspectj.cooka)" ,throwing = "e" )

  public void bind(cookexception e){

   system.out.println( "绑定异常【开始】" );

   system.out.println( "e:" + e.getmessage());

   system.out.println( "绑定异常【结束】" );

  }

}

注意: throwing 的值必须与方法参数名相同。

单元测试:

?

1

2

3

applicationcontext context = new classpathxmlapplicationcontext( "spring-beans.xml" );

cooka cook = (cooka) context.getbean( "cooka" );

cook.make( "" );

输出结果:

绑定异常【开始】
e:煮啥呢???
绑定异常【结束】

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://www.jianshu.com/p/0d7cf0aeb338

查看更多关于谈谈Spring AOP中@Aspect的高级用法示例的详细内容...

  阅读:47次