AOP学习——xml方式

AOP的概念和思想

AOP是面向切面的思想,OOP是面向对象,OOP提供了类与类之间纵向的关系(继承,接口),AOP是横向的关系,我们把它当块板子,这块板子插入一些控制流程,这块板子就可以当成是AOP中的一个切面。所以AOP的本质是在一系列纵向的控制流程中,把那些相同的子流程提取成一个横向的面,我们把纵向流程画成一条直线,然把相同的部分以绿色突出,如下图左,而AOP相当于把相同的地方连一条横线,如下图右。
在这里插入图片描述在这里插入图片描述

AOP中的术语

切面
切面就是切入点和通知的结合
切入点
切入点就是你选择横切的点,切面和目标对象的连接点有很多个,我们可以选择合适的点作为切入点,将切面织入到目标代码中。
通知
通知就是切面要完成的工作,我们要织入的代码。
1.前置通知(before):在目标方法被调用之前调用通知功能,但是无法组织目标方法执行。
2.后置通知(after):在目标方法完成之后调用通知,如果出异常则无法通知。
3.异常通知(after-throwing):在目标方法抛出异常后调用通知。
4.环绕通知(around):目标方法执行前,后通知,可以组织目标方法执行。方法内部可以使用连接点对象来获取被代理方法的所有信息
5.返回通知(after-returning):在目标方法执行结束后调用通知,无论是否出现异常(类似finally块)。

连接点
是在应用执行过程中能够插入切面的一个点。这个点可以是调用方法时、抛出异常时、甚至修改一个字段时。切面代码可以利用这些点插入到应用的正常流程之中,并添加新的行为。
spring只支持方法创建连接点

  • 因为 Spring 基于动态代理,所以 Spring 只支持方法连接点。
  • Spring 缺失对字段连接点的支持,无法让我们更加细粒度的通知,例如拦截对象字段的修改
  • Spring 缺失对构造器连接点支持,我发在 Bean 创建时候进行通知。

织入

  • 织入是将切面应用到目标对象来创建的代理对象过程。
  • 切面在指定的连接点被织入到目标对象中,在目标对象的生命周期中有多个点可以织入

使用方式

XML方式
依赖的包

<dependency>           
 <groupId>org.aspectj</groupId>        
 <artifactId>aspectjweaver</artifactId>
 <version>1.9.4</version>
</dependency>

在 XML 中声明切面
Spring 的 AOP 配置元素简化了基于 POJO 切面声明

AOP配置元素 描述
aop : advisor定义 AOP 通知器
aop : after定义 AOP 后置通知(不管被通知方法是否执行成功)
aop : after-returing定义 AOP after-returing 通知
aop : after-throwing定义 AOP after-throwing 通知
aop : around定义 AOP 环绕通知
aop : aspect定义切面
aop : aspectj-autoproxy启动 @AspectJ 注解驱动的切面
aop : before定义 AOP 前置通知
aop : config顶层的 AOP 配置元素,大多数 aop : * 元素必须包含在 元素内
aop : declare-parents为被通知的对象引入额外接口,并透明的实现
aop : pointcut定义切点

示例:
Java代码

public interface Calc {
    public int Add(int num,int num1);
    public int mins(int num2, int num3);
}

//继承接口Calc
public class Calculate implements Calc{
    @Override
    public int Add(int num, int num1) {
        return num + num1;
    }
    public int mins(int num2, int num3) {
        // 模拟方法执行报错,便于看到after-throwing通知
        int a= 9/0;
        return num2 - num3;
    }
切面类

```java
public Object aroundM(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取连接点代表的方法的签名

        Signature signature=joinPoint.getSignature();
        String methodName= signature.getName();
        Object[] args=joinPoint.getArgs();
        // 调用目标方法
        Object retVal=joinPoint.proceed();
        long start=System.currentTimeMillis();
        // 插入我们自己的逻辑代码
        long timer=System.currentTimeMillis()-start;
        System.out.println("[aroundM]方法["+methodName+"("+ Arrays.toString(args)+")]执行结束,返回值: "+retVal+", 耗时: " + timer + "ms.");
        return retVal;
    }
    public void beforeM() {
        System.out.println("[beforeM] ---- 目标方法开始执行");
    }

    public void afterReturningM(Object retVal) {
        System.out.println("[afterReturningM] ---- 目标方法执行结束,返回值: " + retVal);
    }

    public void afterFinallyM() {
        System.out.println("[afterFinallyM] ---- 方法执行结束");
    }

    public void afterThrowing(Throwable throwable) {
        System.out.println("[afterThrowing] ---- 方法执行出错"+ throwable);
    }

xml配置


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="calc" class="com.lanou3g.spring.aop.Calculate"/>

    <!--    定义切面类-->
    <bean id="calcasp" class="com.lanou3g.spring.aop.CalcAsp"/>
    <aop:config>
        <aop:pointcut id="all_calc" expression="execution(* com.lanou3g.spring.aop.Calculate.*(..))"/>
        <aop:aspect ref="calcasp">
            <aop:around method="aroundM" pointcut-ref="all_calc"/>
            <aop:before method="beforeM" pointcut-ref="all_calc" />
            <aop:after-returning method="afterReturningM" pointcut-ref="all_calc" returning="retVal" />
            <aop:after-throwing method="afterThrowing" pointcut-ref="all_calc" throwing="throwable" />
            <aop:after method="afterFinallyM" pointcut-ref="all_calc" />
        </aop:aspect>
    </aop:config>
</beans>

在 Spring AOP 中,需要使用 AspectJ 的切点表达式来定义切点。

AspectJ 指示器描述
execution ()用于匹配连接点的执行方法 最常用
args ()限制连接点的指定参数为指定类型的执行方法
@args ()限制连接点匹配参数类型由指定注解标注的执行方法
this ()限制连接点匹配 AOP 代理的 Bean 引用为指定类型的类
target ()限制连接点匹配特定的执行对象,目标对象是指定的类型
@target ()限制连接点匹配特定的执行对象,这些对象对应的类要具备指定类型注解
within()限制连接点匹配指定类型,比如哪个包下,或哪个类里面
@within()限制连接点匹配指定注释所标注的类型(当使用 Spring AOP 时,方法定义在由指定的注解所标注的类里)
@annotation限制匹配带有指定注释的连接点

切入点表达式语法:

语法一:单个表达式
切入点表达式语法

execution表达式以 * 号开头,标识了我们不关心的方法返回值的类型。
后我们指定了权限定类名和方法名。(类名和方法名都可以用号通配符代替)
对于方法的参数列表,使用(…)标识切点选择任意的 play( ) 方法,无论入参是什么。
语法二:多个表达式组合
切入点表达式语法
这里我们使用 && 将 execution( ) 和 within( ) 连接起来,形成的 and 关系。同理也可以使用 || 或关系、!非关系

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值