AOP切面编程

前言:

AOP(Aspect Oriented Programming)是一种设计思想,是软件设计领域中的面向切面编程,它是面

向对象编程的一种补充和完善,它以通过预编译方式和运行期动态代理方式实现在不修改源代码的情况

下给程序动态统一添加额外功能的一种技术

Spring中的AOP

AOP在spring中又叫“面向切面编程”,它可以说是对传统我们面向对象编程的一个补充,从字面上顾名思义就可以知道,它的主要操作对象就是“切面”,所以我们就可以简单的理解它是贯穿于方法之中,在方法执行前、执行时、执行后、返回值后、异常后要执行的操作。相当于是将我们原本一条线执行的程序在中间切开加入了一些其他操作一样。

在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能应用在哪里,以什么方式应用,并且不必修改受影响的类。这样一来横切关注点就被模块化到特殊的类里——这样的类我们通常就称之为“切面”。

通知

每一个横切关注点上要做的事情都需要写一个方法来实现,这样的方法就叫通知方法。

前置通知:在被代理的目标方法执行

返回通知:在被代理的目标方法成功结束后执行(寿终正寝

异常通知:在被代理的目标方法异常结束后执行(死于非命

后置通知:在被代理的目标方法最终结束后执行(盖棺定论

环绕通知:使用try...catch...finally结构围绕整个被代理的目标方法,包括上面四种通知对应的所

有位置

导包

在pom文件加入以下注解,使用maven导入需要的jar包

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.1</version>
        </dependency>

接口calculator

创建一个计算器接口类calculator

public interface Calculator {
    int add(int i, int j);

    int sub(int i, int j);

    int mul(int i, int j);

    int div(int i, int j);
}

实现类calculator

@Component
public class CalculatorImpl implements Calculator {

    @Override
    public int add(int i, int j) {
        int result = i + j;
        System.out.println("i+j="+result);
        return result;
    }

    @Override
    public int sub(int i, int j) {
        int result = i - j;
        System.out.println("i-j="+result);
        return result;
    }

    @Override
    public int mul(int i, int j) {
        int result = i * j;
        System.out.println("i*j="+result);
        return result;
    }

    @Override
    public int div(int i, int j) {
        int result = i / j;
        System.out.println("i/j="+result);
        return result;
    }
}

切面类LoggerAspect

//在切面中,通过指定注解将方法标识为通知方法
//before 前置通知,在目标对象方法执行之前执行
@Component
@Aspect  //将当前组件标识为切面
public class LoggerAspect {
    @Before("execution(public int com.spring.aop.annotation.Calculator.add(int,int))")
    public void beforeAdviceMethod() {
        System.out.println("LoggerAspect:前置通知:add");
    }
}

Spring配置文件

<?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:context="http://www.springframework.org/schema/context"
       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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--
        AOP的注意事项
        切面类和目标类都交给IOC容器管理
        切面类必须通过@Aspect注解标识为一个切面
    -->
    <context:component-scan base-package="com.spring.aop.annotation"></context:component-scan>
    <!--    开启基于注解的AOP功能-->
    <aop:aspectj-autoproxy/>
</beans>

测试

由于切面类中写死了,指定了类和只有add方法有前置通知,我们把他改成*通配符,再跑一次

    @Before("execution(* com.spring.aop.annotation.*.*(..))")
    public void beforeAdviceMethod2(JoinPoint joinpoint) {
        //获取连接点对应方法名
        Signature signature = joinpoint.getSignature();
        System.out.println("LoggerAspect:前置通知:"+signature.getName());
    }

这次加法和减法都有前置通知了。

修改对比:

@Before("execution(public int com.spring.aop.annotation.Calculator.add(int,int))")
@Before("execution(* com.spring.aop.annotation.Calculator.* (..))")

用*号代替“权限修饰符”和“返回值”部分表示“权限修饰符”和“返回值”不限

在包名的部分,一个“*”号只能代表包的层次结构中的一层,表示这一层是任意的。

例如:*.Hello匹配com.Hello,不匹配com.atguigu.Hello

在包名的部分,使用“*..”表示包名任意、包的层次深度任意

在类名的部分,类名部分整体用*号代替,表示类名任意

在类名的部分,可以使用*号代替类名的一部分

例如:*Service匹配所有名称以Service结尾的类或接口

在方法名部分,可以使用*号表示方法名任意

在方法名部分,可以使用*号代替方法名的一部分

例如:*Operation匹配所有方法名以Operation结尾的方法

在方法参数列表部分,使用(..)表示参数列表任意

在方法参数列表部分,使用(int,..)表示参数列表以一个int类型的参数开头

在方法参数列表部分,基本数据类型和对应的包装类型是不一样的

切入点表达式中使用 int 和实际方法中 Integer 是不匹配的

在方法返回值部分,如果想要明确指定一个返回值类型,那么必须同时写明权限修饰符

例如:execution(public int ..Service.*(.., int)) 正确

例如:execution(* int ..Service.*(.., int)) 错误

后置通知After

前面测试的是前置Before,在代理方法执行之前执行,如下演示后置通知,在切面类添加👇

   @After("execution(* com.spring.aop.annotation.*.*(..))")
    public void AfterAdviceMethod(JoinPoint joinpoint) {
        Signature signature = joinpoint.getSignature();
        System.out.println("LoggerAspect:后置通知:" + signature.getName());
    }

测试一下

div中分母传参0进去,此时抛出异常,但后置通知依然输出在控制台,可见后置通知是在对象方法的finally字句中执行的。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring AOP切面编程是一种通过配置的方式,实现在Spring开发中的AOP功能。切面由切点和增强组成,切点用于定义在哪些连接点上应用增强,而增强指的是在连接点上要执行的逻辑操作。SpringAOP强大之处在于不需要通过代码,只需要使用注解或XML配置就可以完成相同的功能。 在学习Spring开发的AOP面向切面编程时,需要导入相应的jar包,例如com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar。切面编程是一种实现横切关注点的重用和集中管理的技术,它可以让我们更加方便地实现一些横切关注点,如日志记录、事务管理等,并将其与业务逻辑分离开来,提高代码的复用性和可维护性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [spring AOP切面编程](https://blog.csdn.net/weixin_43525993/article/details/107902045)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [SpringAOP切面编程依赖jar包.rar](https://download.csdn.net/download/weixin_44888416/12374630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃香蕉的阿豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值