aop

aop api

1、 熟悉api

2、 参透运行机制

3、 收缩、逐步扩展

spring aop

1、 api怎么写?

Chapter 6. Aspect Oriented Programming with Spring

切面: PointCut(切点: joinPoint的集合)+ 通知(Advice)方式+ 通知(Advice)增强代码

定义切点:

定义切点

注解定义Aspect

1、定义Pointcut切点

2、 定义增强方法

3、 通知方式将 增强方法和Pointcut绑定起来

4、 完整示例

@Aspect 
public class BeforeExample {


// 切面 = 增强方法+通知方式+Pointcut切点
  @Before("execution(* com.xyz.myapp.dao.*.*(..))")  # 通知方式+Pointcut切点
  public void doAccessCheck() { // 	增强方法
    // ...
  }

}

5、 单独定义Pointcut切点

@Pointcut("execution(* transfer(..))")// the pointcut expression
private void anyOldTransfer() {}// the pointcut signature

schema定义Aspect

<aop:aspect>
定义Aspect类, 类似于@AspectJ注解的类



<aop:config>
  <aop:aspect id="myAspect" ref="aBean"> # 将通知增强类和 通知方式+Pointcut绑定
# 1、 定义Pointcut
# 2、 将Pointcut和通知增强类的增强方法绑定; 并指定通知方式

    ...
  </aop:aspect>
</aop:config>

<bean id="aBean" class="...">  # 通知增强类; 定义了通知增强方法
  ...
</bean>

schema定义Aspect完整示例

<aop:config>

# 将通知增强类和 通知方式+Pointcut绑定
  <aop:aspect id="concurrentOperationRetry" ref="concurrentOperationExecutor">

# 定义Pointcut(切点)
    <aop:pointcut id="idempotentOperation"
        expression="execution(* com.xyz.myapp.service.*.*(..))"/>
       
  #  将增强类的增强方法和切点绑定且指定通知方式
    <aop:around
       pointcut-ref="idempotentOperation"
       method="doConcurrentOperation"/>
  
  </aop:aspect>

</aop:config>

# 增强类
<bean id="concurrentOperationExecutor"
  class="com.xyz.myapp.service.impl.ConcurrentOperationExecutor">
     <property name="maxRetries" value="3"/>
     <property name="order" value="100"/>  
</bean>


拦截请求

借助spring aop拦截请求

Pointcut 拦截注解、 拦截方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值