关于Spring中AOP的一些理解

简介:关于AOP的一些笔记。

1.AOP简介

如果我们想对一个类进行增强,最常见的方式有两种:

  • 继承:通过继承,可以扩展父类的功能,从而得到更强大的类——子类
  • 代理:使用代理,让方法执行前后,再做一些额外的事情,从而获得更强大的类——代理类。

很明显,继承是一种纵向的关系,而代理更偏向于一种横向的关系,如我们想要一个方法执行前后都输出一些日志,使用代理的方式就更适合。
那么那些被增强的方面,我们就可以称为一个切面,比如一个函数,可以给他加一个“性能检测”切面,也可以再加一个“日志管理”切面,我们就把这种面向切面的编程方式叫做AOP(Aspect Oriented Programming)。
AOP常用于对同一层次对象公共行为进行抽取,这样对象就只要关心自己的核心代码,而不需要关心“杂事”。同时,这也极大提高了程序的可复用性(一个切面添加到很多的模块上),可扩展性(核心代码与其他代码不耦合,可以很方便得增减)。

2.AOP名词解释

既然要进行切面编程,首先得明确几个主体:
1.目标类(taget) :也就是我们想要加强的那个类,也可以称为被代理类。该类有很多方法,因为一般增强都是对方法进行增强,所以我们把这些方法称为连接点(JoinPoint),显然,目标类的所有方法都是可能被增强的,所以都是连接点。而我们把那些已经被增强的方法叫做切入点(PiontCut)
2.通知(advice):既然我们有了目标,那么如何增强,就是通知决定的了,通知也是函数,被用于加强某些切入点,在切入点执行前后或异常(等)时执行。
3.切面类(Aspect):这些通知基本上都是用于同一个功能上,所以我们把这些通知都放进一个类里,这个类就是切面类。
4.织入(Weaving):这是一个动词,我们现在切面也有了,目标也有了,剩下的事情就是把切面类的通知给加强到目标类的连接点上,我们把这个过程就叫做织入。

再介绍一下通知的种类:
Before:在目标方法执行前执行。
AfterReturning:在目标方法return后执行。
AfterThrowing:在目标方法抛出异常后执行。
After:在目标方法执行后(无论正常结束还是异常结束)执行。
Around:环绕通知,功能强大,可以做上面4个能做的所有事情。
先记着这些类的触发条件,而当一个条件可以触发多个通知时,他们的执行顺序,在后面再专门讨论。


下面就开始通过程序来演示如何使用AOP

3.使用AOP

首先来两个类:目标类切面类
目标类:

public class Student {
    public void eat() {
        System.out.println("吃饭");
    }
    public boolean isStudy() {
        return true;
    }
    public void play() {
        System.out.println("玩游戏");
    }
}

切面类:存放的就是上述的那些通知(方法)。
我们使用的是AspectJ的方式,所以方法的名字是任意的,但是为了方便,我们会把方法名取成它相对应的功能名。
Before方法:参数JoinPoint joinPoint即为那个被加强的连接点。

AfterReturning:因为该方法在目标方法return后执行,所以有可能要对其返回值进行操作。为此,我们经常会加上一个参数——Object ret来表示那个返回的对象(此处的void会被当成null处理)。

AfterThrowing:在目标方法抛出异常后执行,为了能处理抛出的异常对象,我们通常会再加一个参数——Throwable throwable

After:在目标方法执行后(无论正常结束还是异常结束)执行。

Around:该函数因为是手动调用目标方法joinPoint.proceed();
(注意joinPoint是ProceedingJoinPoint),所以我们可以在很自由的位置来添加代码,从而达到上面所有的功能。因为有可能要返回处理后的目标方法的返回对象,所以该方法返回值一般为Object。

public class MyAspect {
    public void before(JoinPoint joinPoint){
        System.out.println("before");
    }
    public void aftherReturning(JoinPoint joinPoint,Object ret){
        System.out.println("after-returning" + "--返回了:" + ret);
    }
    public void throwing(JoinPoint joinPoint,Throwable throwable) {
        System.out.println("异常是:" + throwable.getMessage());
    }
    public void after(JoinPoint joinPoint){
        System.out.println("after");
    }

	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object o = null;
        System.out.println("Before");
        try {
            o = joinPoint.proceed();
            System.out.println("AfterReturning");
        } catch (Throwable t){
            System.out.println("AfterThrowing");
        } finally {
            System.out.println("After");
        }
        return o;
    }


}

接下来的事情,就是把这两个类配置到Spring中,并且产生联系
我们依然从xml和注解2个方面来配置。

3.1 AOP基于xml

首先,先把两个类注册到容器中

    <bean id="myAspect" class="com.hello.pojo.MyAspect"></bean>
    <bean id="student" class="com.hello.pojo.Student"></bean>

剩下的就是要让这两个类进行联系(注意:这里的xml是需要增加一些命名空间的)。
我们把所有的联系,都写在 <aop:config>这个标签中。
对于目标类和切面类关联这同样一件事,我们有两种不同的描述方式:
1:把通知增强到连接点上。
2:在连接点上加上需要的通知。
这两种方式无非是主语不同,效果是一样的,而XML采用的第一种说法——把通知增强到连接点(目标类的方法)上
所以,xml的层次结构看起来是这样的:

    <aop:config>
        <aop:aspect ref="myAspect">
			各类通知
        </aop:aspect>
    </aop:config>

<aop:aspect ref="myAspect">表示切面类,里面将放上子标签——各种类型的通知,ref属性表示我们要引用哪个切面类,这里当然是引用我们前面配进来的那个id为“myAspect”的bean。
我们马上要写的通知类子标签,里面有个method属性,很明显就是要填方法的名字,让方法和该标签行成映射;既然要加强方法,那么肯定还要明确该通知要作用于哪些方法上,这里是通过pointcut属性来确定的,属性值我们通过写形如:==execution(* com.hello.pojo..(…))==这样的表达式来表示方法。但是,有时候很多通知要作用的方法,都是同一组,为了减少该表达式的重复书写,我们会把这组方法,专门放到一个标签中——

<aop:pointcut expression="execution(* com.hello.pojo.*.*(..))" id="myPointcut"/>

并且给它取一个id,这样通知标签想要使用的时候,就不需要
pointcut = "execution(* com.hello.pojo.*.*(..))"
而是可以使用
pointcut-ref = "myPointcut"
整体效果如下:

    <bean id="myAspect" class="com.bdqn.pojo.MyAspect"></bean>
    <bean id="student" class="com.bdqn.pojo.Student"></bean>
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:pointcut expression="execution(* com.bdqn.pojo.*.*(..))" id="myPointcut"/>
            <aop:before method="before" pointcut-ref="myPointcut"/>
            <aop:after-throwing method="throwing" pointcut-ref="myPointcut" throwing="throwable"/>
            <aop:after-returning method="aftherReturning" pointcut-ref="myPointcut" returning="ret"/>
            <aop:around method="around" pointcut-ref="myPointcut"/>
            <aop:after method="after" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>

这里可以看出,抛出异常通知和after-returning通知,各自多了些属性,这是因为这两个方法有额外的参数,而那些属性就是对应着这些参数的。
在这里插入图片描述
运行结果如下:
在这里插入图片描述

3.2 AOP基于注解

下面采用注解的方式来试下。
首先回顾一下xml的几个模块
1:把切面类和目标类放入容器
2:让切面类成为切面类:
在这里插入图片描述
3:把通知方法放入xml中,并声明其要作用到哪些函数上
在这里插入图片描述
4:当然,还有一个声明切入点的标签:
在这里插入图片描述
现在我们就从这几个角度出发,来用注解代替这些xml语句。
现在xml里的配置可以都删了,但要加上以下代码来使得注解生效:
在这里插入图片描述
目标类的注解很简单,使用@Component加载到容器中就可以了
在这里插入图片描述
切面类则不仅要加载到容器中,还要声明自己是“切面类”。
在这里插入图片描述
然后写一个空函数,来代表某组连接点:
在这里插入图片描述
这个即代替了原先的
在这里插入图片描述
最后,就只需要在各种通知方法上,加上相应类型的注释,并表明需要作用的方法即可了:
在这里插入图片描述
切面类完整代码如下:

@Component("myAspect")
@Aspect
public class MyAspect {

    @Pointcut("execution(* com.bdqn.pojo.*.*(..))")
    public void myPointCut() {
    }
    @Before(value = "myPointCut()")
    public void before(JoinPoint joinPoint){
        System.out.println("before");
    }
    @AfterReturning(value = "myPointCut()", returning = "ret")
    public void aftherReturning(JoinPoint joinPoint,Object ret){
        System.out.println("after-returning" + "--返回了:" + ret);
    }
    @AfterThrowing(value = "myPointCut()", throwing = "throwable")
    public void throwing(JoinPoint joinPoint,Throwable throwable) {
        System.out.println("异常是:" + throwable.getMessage());
    }
    @Around(value = "myPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object o = null;
        System.out.println("$Before");
        try {
            o = joinPoint.proceed();
            System.out.println("$AfterReturning");
        } catch (Throwable t){
            System.out.println("$AfterThrowing");
        } finally {
            System.out.println("$After");
        }
        return o;
    }
    @After(value = "myPointCut()")
    public void after(JoinPoint joinPoint){
        System.out.println("after");
    }

}

注解完整配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"

        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    ">

    <context:component-scan base-package="com.hello.pojo" />
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

xml完整配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"

        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    ">

    

    <bean id="myAspect" class="com.hello.pojo.MyAspect"></bean>
    <bean id="student" class="com.hello.pojo.Student"></bean>
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:pointcut expression="execution(* com.bdqn.pojo.*.*(..))" id="myPointcut"/>

            <aop:before method="before" pointcut-ref="myPointcut"/>
            <aop:after-throwing method="throwing" pointcut-ref="myPointcut" throwing="throwable"/>
            <aop:after-returning method="aftherReturning" pointcut-ref="myPointcut" returning="ret"/>
            <aop:around method="around" pointcut-ref="myPointcut"/>
            <aop:after method="after" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

4.AOP通知的执行顺序

AOP通知的执行顺序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值