Spring 实战 七: 声明切面的两种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aimeimeiTS/article/details/81278860

使用注解

  1. @After
  2. @AfterReturning
  3. @AfterThrowing
  4. @Around
  5. @Before

使用 javaConfig 时需要在配置类上加上 @EnableAspectJAutoProxy 注解,xml 时使用:

环绕通知

image.png
args 限定符,表明床底给 play 方法的 int 型参数也会传递到通知中。
image.png
image.png

通过注解引入新功能(新方法)

  1. 通过 spring aop,我们可以为 bean 引入新方法
  2. 当引入接口的方法被调用时,代理会把此调用委托给实现了接口的某个其他对象
  3. 借助 aop 引入功能,我们可以不必在设计上妥协或者侵入性的改变现有的实现

@DeclareParents 注解可将新接口引入到现有 bean 中
image.png
value:指明哪种类型的 bean 要引入该接口
defaultImpl:为引入功能提供实现的类
@DeclareParents 注解的成员指明了要引入的接口

在 xml 中声明切面

<aop:advisor>
<aop:after>
<aop:after-returning>
<aop:after-throwing>
<aop:around>
<aop:aspect>
<aop:aspectj-autoproxy>
<aop:before>
<aop:config> 顶层 aop 配置元素,大多数 <aop:*>都为其子属性
<aop:declare-parents>
<aop:pointcut>

image.png

猜你喜欢

转载自blog.csdn.net/aimeimeiTS/article/details/81278860
今日推荐