Spring普通AOP开发(XML方式)

这篇博客详细介绍了Spring AOP的开发,包括使用AOP的目的,如方法增强,以及核心概念,如连接点、切入点、通知等。通过实例演示了如何创建项目、配置XML,实现前置、后置、环绕和异常通知。最后,提供了异常处理的测试用例。
摘要由CSDN通过智能技术生成

一、使用AOP目的

使用AOP的目的:就是对类里面的方法进行增强

  • 前置增强:在方法执行之前加入相关代码
  • 后置增强:在方法执行之后加入相关代码
  • 环绕增强:前置增强和后置增强
  • 异常增强:在目标方法发生异常时加入相关代码

二、AOP开发中的概念

  1. JoinPoint(连接点):所谓连接点是指哪些被拦截的点,而spring 中这些点就是指方法,因为spring只支持方法类型的连接点。
  2. PointCut(切入点):所谓切入点就是指我们要对那些JoinPoint 进行拦截的定义。
  3. Advice(通知/增强):所谓通知/增强,就是指拦截到JoinPoint后需要完成的事情,它分为前置通知/增强,后置通知/增强,异常通知/增强,最终通知/增强,环绕通知/增强(切面要完成的功能)
  4. Introduction(引介):引介是一种特殊的Advice,在不修改代码的前提下,引介可以在运行期为类动态的添加一些方法或Field。
  5. Target(目标):代理对象的目标对象(要增强的类)
  6. Weaving(织入):把 Advice 应用到 Target 的过程
  7. Proxy(代理):一个类被 AOP 注入增强后,就产生了一个结果代理类
  8. Aspect(切面):是 PointCut 和Advice(Introduction)的结合

三、示例

1. 创建项目并导包

在这里插入图片描述
在这里插入图片描述

2. 创建Person

在这里插入图片描述

3. 创建applicationContext.xml

<!-- 导入头文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
    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
         http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

4. 前置通知

  • 创建MyBeforeAdvise
    在这里插入图片描述
  • 配置applicationContext.xml
    在这里插入图片描述
  • 测试
    在这里插入图片描述

5. 后置通知

  • 创建MyAfterAdvise
    在这里插入图片描述
  • 配置applicationContext.xml
    在这里插入图片描述

6. 环绕通知

  1. 第一种环绕通知
     |-- 创建MyBeforeAdvise、MyAfterAdvise
    在这里插入图片描述
    在这里插入图片描述
     |-- 配置applicationContext.xml
    在这里插入图片描述
     |-- 测试
    在这里插入图片描述
  2. 第二种环绕通知
     |-- 创建MyAroundAdvise
package com.lasing.advise;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* 环绕通知
* */
public class MyAroundAdvise implements MethodInterceptor{
     /**
      * MethodInvocation
      * 目标方法执行对象
      * */
     @Override
     public Object invoke(MethodInvocation invocation) throws  Throwable {
           before();
           Object proceed = invocation.proceed();
           after();
           return proceed;
     }
     /**
      * 执行前做的事
      * */
     public void before() {
           System.out.println("饭前水果...");
     }
     /**
      * 执行后做的事
      * */
     public void after() {
           System.out.println("饭后甜点...");
     }
}

 |-- 配置applicationContext.xml
在这里插入图片描述

 |-- 测试
在这里插入图片描述

7. 异常通知

  • 手动设置一个异常
    在这里插入图片描述
  • 创建MyExceptionAdvise
    在这里插入图片描述
  • 配置applicationContext.xml
    在这里插入图片描述
  • 测试
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值