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 通知" );
}
}