AOP
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率
- 优势:减少重复代码,提高开发效率,维护方便
- 实现方法:动态代理
Spring中AOP
XML配置
- 使用
aop:config
标签开始配置 - 使用
aop:aspect
标签表明配置切面
属性:
id
给前面提供一个唯一标示
ref
:指定通知类bean的id - 在
aop:aspect
内部配置通知的类型
前置/后置通知
例如:通过printlog方法在切入点(被代理类中的被代理方法)执行前执行,也就是前置通知
aop:before
标示配置前置通知
aop:after-returning
标示配置后置通知
aop:after-throwing
标示配置异常通知
aop:after
标示配置最终通知
属性:
method
用于指定代理类中哪个方法前置通知
printcut
用于指定切入点表达式,是指对业务层中那些方法进行增强
关键字:execution
切入点表达式:访问修饰符 返回值 包名.类名.方法名(参数列表)
全通配写法:* ….*(…)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- sop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut id="pc1" expression="execution(* demo.service.impl.*.*(..))"/>
<!-- 配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 配置前置通知 -->
<aop:before method="printLog" pointcut="execution(* *..*.*(..))"></aop:before>
<!-- 配置最终通知 -->
<aop:after method="printLog" pointcut-ref="pc1"></aop:after>
</aop:aspect>
</aop:config>
环绕通知
<aop:around>
Spring提供了一个接口,ProceedingJoinPoint
该接口有一个方法proceed()
,此方法相当于明确调用切入点方法。
将通过xml配置的方式,移入了对应通知方法中。
public Object aroundLog(ProceedingJoinPoint pjp){
Object rtValue;
try{
Object[] args = pjp.getArgs();
System.out.println("前置通知");
rtValue = pjp.proceed(args);
System.out.println("后置通知");
return rtValue;
}catch (Throwable t){
System.out.println("异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("最终通知");
}
}
注解AOP
@EnableAspectJAutoProxy // 开启自动代理AOP
加在config类上
@Aspect
在切面类上加注解,定义为一个切面类
@Component
@Aspect//当前类是一个切面类
public class Logger {}
@Pointcut
方法没有方法体,在其他注解调用时直接输入方法名即可
@Pointcut("execution(* demo.service.impl.*.*(..))")
private void pc1(){}
@Before&@AfterReturning&@AfterThrowing@After
@Before("pc1()")
public void printLog(){
System.out.println("前打印日志----------");
}
@AfterReturning("pc1()")
public void afterReturnPrintLog(){
System.out.println("后置打印日志----------");
}
@AfterThrowing("pc1()")
public void afterThrowPrintLog(){
System.out.println("异常打印日志----------");
}
@After("pc1()")
public void afterPrintLog(){
System.out.println("结束打印日志----------");
}
但是这种方式可能会导致顺序出错,可以看出@After
在@AfterReturning
之前执行
前打印日志----------
执行查找方法
结束打印日志----------
后置打印日志----------
Employee{id=10006, name='c'}
@Around
环绕打印不会出现结果顺序出错
@Around("pc1()")
public Object aroundLog(ProceedingJoinPoint pjp){
Object rtValue;
try{
Object[] args = pjp.getArgs();
System.out.println("前置通知");
//明确调用业务层方法
rtValue = pjp.proceed(args);
System.out.println("后置通知");
return rtValue;
}catch (Throwable t){
System.out.println("异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("最终通知");
}
}
前置通知
执行查找方法
后置通知
最终通知
Employee{id=10006, name='c'}