AOP
AOP(Aspect oriented Programming)面向切面编程,一种编程范式,指导开发者如何组织程序结构
作用:在不惊动原始设计的基础上为其进行功能增强
Spring理念:无入侵式/无侵入式
概念
连接点(JoinPoint ):程序执行过程中的任意位置粒度为执行方法、抛出异常、设置变量等在SpringAOP中,理解为方法的执行
切入点( Pointcut ):匹配连接点的式子
在SpringAOP中,一个切入点可以只描述一个具体方法,也可以匹配多个方法一个具体方法:com.itheima.dao包下的BookDao接口中的无形参无返回值的save方法
匹配多个方法:所有的save方法,所有的get开头的方法,所有以Dao结尾的接口中的任意方法,所有带有一个参数的方法通知(Advice):在切入点处执行的操作,也就是共性功能在SpringAoP中,功能最终以方法的形式呈现
通知类:定义通知的类
切面(Aspect):描述通知与切入点的对应关系
将所有方法要做同样处理的方法进行抽取,创建一个通知类
将共性创建一个方法——通知
将需要执行通知的方法找出来——切入点
将通知与切入点绑定——切面
案例
定义通知类
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
public class MyAdvice {
//设置切入点,要求配置在方法上方
//定义切入点
@Pointcut("execution(void com.itheima.dao.BookDao.update())")
private void pt(){}
//设置在切入点pt()的前面运行当前操作(前置通知)
@Before("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}
配置类中加注解启动AOP
@Configuration
@ComponentScan("com.itheima")
//开启注解开发AOP功能
@EnableAspectJAutoProxy
public class SpringConfig {
}
切入点表达式
//切入点表达式:
// @Pointcut("execution(void com.itheima.dao.BookDao.update())")
// @Pointcut("execution(void com.itheima.dao.impl.BookDaoImpl.update())")
// @Pointcut("execution(* com.itheima.dao.impl.BookDaoImpl.update(*))") //no
// @Pointcut("execution(void com.*.*.*.update())")
// @Pointcut("execution(* *..*(..))")
// @Pointcut("execution(* *..*e(..))")
// @Pointcut("execution(void com..*())")
// @Pointcut("execution(* com.itheima.*.*Service.find*(..))")
//执行com.itheima包下的任意包下的名称以Service结尾的类或接口中的save方法,参数任意,返回值任意
@Pointcut("execution(* com.itheima.*.*Service.save(..))")
AOP通知类型
AOP通知描述了抽取的共性功能,根据共性功能抽取的位置不同,最终运行代码时要将其加入到合理的位置
AOP通知共分为5种类型
前置通知
后置通知
环绕通知(重点)
返回后通知(了解
抛出异常后通知(了解 )
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(void com.itheima.dao.BookDao.update())")
private void pt(){}
@Pointcut("execution(int com.itheima.dao.BookDao.select())")
private void pt2(){}
//@Before:前置通知,在原始方法运行之前执行
// @Before("pt()")
public void before() {
System.out.println("before advice ...");
}
//@After:后置通知,在原始方法运行之后执行
// @After("pt2()")
public void after() {
System.out.println("after advice ...");
}
//@Around:环绕通知,在原始方法运行的前后执行
// @Around("pt()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around before advice ...");
//表示对原始操作的调用
Object ret = pjp.proceed();
System.out.println("around after advice ...");
return ret;
}
// @Around("pt2()")
public Object aroundSelect(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around before advice ...");
//表示对原始操作的调用
Integer ret = (Integer) pjp.proceed();
System.out.println("around after advice ...");
return ret;
}
//@AfterReturning:返回后通知,在原始方法执行完毕后运行,且原始方法执行过程中未出现异常现象
// @AfterReturning("pt2()")
public void afterReturning() {
System.out.println("afterReturning advice ...");
}
//@AfterThrowing:抛出异常后通知,在原始方法执行过程中出现异常后运行
@AfterThrowing("pt2()")
public void afterThrowing() {
System.out.println("afterThrowing advice ...");
}
}