您的位置:首页 > 运维架构

AOP 详解

2020-07-10 22:20 260 查看

AOP

相关术语

开发阶段:由开发人员完成
编写核心业务代码
把公用代码抽取出来
在配置文件中(XML) 声明切入点与通知间的关系,即切面。 AOP由编程人员来做。

运行阶段:由Spring框架完成
框架监控切入点方法的执行, 监控到切入点被运行,便使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的相应位置,将通知对应的功能织入,完成完整的代码逻辑运行。

连接点(joinpoint):实现层里所有的方法都叫连接点(spring只支持方法类型的连接点)

切入点(pointcut):对哪写joinpoint进行拦截,增强后的方法

切入点一定是连接点,连接点不一定是切入点。

通知类型

环绕通知:整个invoke方法在执行就称为环绕通知

前置通知(beforePrint)
后置通知 (afterReturningPrint)
异常通知 (afterThrowingPrint)
最终通知 (afterPrint)

其他配置

<!--配置spring创建容器时需要扫描的包 -->
<context:component-scan base-package=" "></context:component-scan>

<!-- 配置spring开启注解AOP的支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

类的配置

<bean id="自定义"  class="类的全限定类名"></bean>

AOP配置

<aop:config>
!--配置切面-->
<aop:aspect id="自定义" ref="类中bean的id">
<!--配置通知的类型,并且建立通知方法和切入点方法的关联-->

<!--配置前置:在切入点方法执行之前执行-->
<aop:before method="需要被增强的方法名" pointcut="execution(切入点表达式)"></aop:before>
<!--配置后置:在切入点方法正常执行之后值 它和异常通知永远只能执行一个-->
<aop:after-returning method="需要被增强的方法名" pointcut="execution(切入点表达式)"></aop:after-returning>
<!--配置异常:在切入点方法执行产生异常之后执行-->
<aop:after-throwing method="需要被增强的方法名" pointcut="execution(切入点表达式)"></aop:after-throwing>
<!--配置最终:无论切入点是否正常执行它都会在其后面执行-->
<aop:after method="需要被增强的方法名" pointcut="execution(切入点表达式)"></aop:after>

<!--配置环绕通知-->
<aop:around method=""  pointcut-ref=" "></aop:around >
<!-- ------------------------------------------------------------------------------------- -->
<!-- 也可以将上述配置通知中的pointcut修改为pointcut-ref="自定义通用切入点的id"-- -->
<aop:pointcut id="自定义通用切入点id" expression="execution(全限定类名)"></aop:pointcut>
<!-- id属性用于指定表达式的唯一标识  expression属性用于指定表达式内容,此标签写在aop:aspect标签内部只能当前切面使用(它也可以卸载aop:aspect外面,就变成所有切面可用) -->
</aop:aspect>
</aop:config>

配置环绕通知

问题:
当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了.
分析:
通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用
解决:
Spring框架为我们提供了一个接口:ProceedingJoinPoint 该接口有一个procedd()方法,此方法就相当于明确调用切入点方法. 该接口可以作为环绕通知的方法参数.

spring中的环绕通知:
它是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式.

@Around()
public Object aroundPrint(ProceedingJoinPoint pjp){
Object rtValue = null;
try{
Object[] args = pjp.getArgs();//得到方法执行所需的参数
System.out.println("aroundPrint方法开始记录日志了....前置");
rtValue = pjp.proceed(args); //明确调用业务层方法(切入点方法)
System.out.println("aroundPrint方法开始记录日志了....后置");
return rtValue;
}catch (Throwable t){
System.out.println("aroundPrint方法开始记录日志了....异常");
throw new RuntimeException(t);
}finally {
System.out.println("aroundPrint方法开始记录日志了....最终");
}
}

切入点(pointcut)表达式的写法

一 . 标准写法 (访问修饰符 返回值 包名.包名…类名.方法名(参数列表))
参数列表:
可以直接写数据类型
基本类型直接写名称 [int]
引用类型写包名.类名的方式 [java.lang.String]

public void com.canda.service.impl.AccountServiceImpl.saveAccount()

二 . 全通配写法(不推荐)
访问修饰符可以省略

* *..*.*(..)

三 实际开发中切入点表达式的通常写法

* com.canda.service.impl.*.*(..)

切面类的格式

@Component("")
@Aspect //表示当前类是一个切面类
public class 类名{
@Pointcut("execution(* com.canda.service.impl.*.*(..))")
private void 自定义通用切入点id(){}

//前置
@Before()
public void beforePrint(){
System.out.println("前置通知类中的方法执行了");
}

//后置
@AfterReturning
public void afterReturningPrint(){
System.out.println("后置通知类中的方法执行了");
}

//异常
@AfterThrowing
public void afterThrowingPrint(){
System.out.println("前置通知类中的方法执行了");
}

//最终
@After
public void afterPrint(){
System.out.println("最终通知类中的方法执行了");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: