AOP

AOP实现
公式:aop=通知方法(5种)+切入点表达式(4种)
通知:
1.before通知: 在执行目标方法之前执行
2.after通知: 无论什么时候程序执行完成后执行
3.afterReturning通知: 在目标方法执行之后执行
4.afterThrowing通知: 在目标方法执行之后报错时执行
以上不能控制目标方法是否执行,一般用来记录程序的执行状态。
5.around通知: 在目标方法执行前后执行 (功能强大)
因为环绕通知可以控制程序执行的轨迹
切入点
1.bean(“bean的ID”) 粒度:按Bean匹配 当前Bean中的方法都会执行方法
2.within(“包名.类名”)
3.execution(“返回值类型 包名.类名.方法名(参数列表)”)
4.@annotation(“包名.类名”)

package com.jt.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect//标识切面
@Component//交给spring容器管理
public class CacheAop {
    //切面=切入点表达式+通知方法
   // @Pointcut("bean(itemCatServiceImpl)")
   // @Pointcut("within(com.jt.service.ItemCatServiceImpl)")
   // @Pointcut("within(com.jt.service.*)")//.*  ..*
    @Pointcut("execution(* com.jt.service..*.add*(*))")
    //在com.jt.service下的所有子孙类,以add开头的方法,任意参数类型
    public void pointCut(){

    }
    @Before("pointCut()")
    public void before(){
        System.out.println("before 通知" );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值