美文网首页
Spring AOP(完结)

Spring AOP(完结)

作者: 拼搏男孩 | 来源:发表于2020-02-28 23:37 被阅读0次

    一、第五种方式

    1、AOP的相关概念

    • 横切关注点:对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
    • Aspect(切面):通常是一个类,里面可以定义切入点和通知
    • JointPoint(连接点):程序执行过程中明确的点,一般是方法的调用。被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
    • Advice(通知):AOP在特定的切入点上执行的增强处理,有before(前置),after(后置),afterReturning(最终),afterThrowing(异常),around(环绕)
    • Pointcut(切入点):就是带有通知的连接点,在程序中主要体现为书写切入点表达式
    • weave(织入):将切面应用到目标对象并导致代理对象创建的过程
    • introduction(引入):在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
    • AOP代理(AOP Proxy):AOP框架创建的对象,代理就是目标对象的加强。Spring中的AOP代理可以使JDK动态代理,也可以是CGLIB代理,前者基于接口,后者基于子类
    • 目标对象(Target Object): 包含连接点的对象。也被称作被通知或被代理对象。POJO

    2、第五种方式

    Student.class

    package com.qianfeng.aop05;
    
    
    public class Student implements Person {
        @Override
        public boolean eat() {
            System.out.println("I can eat");
            return true;
        }
        @Override
        public String drink() {
            System.out.println(1/0);
            System.out.println("I can drink");
            return null;
        }
    }
    
    

    beans5.xml

    <?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: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/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
        <bean id="st" class="com.qianfeng.aop05.Student" />
        <bean id="my" class="com.qianfeng.aop05.MyAspect" />
        <aop:config proxy-target-class="true">
            <aop:aspect ref="my">
                <aop:pointcut id="pt" expression="(execution(boolean com.qianfeng.aop05.*.*(..))) or (execution(java.lang.String com.qianfeng.aop05.*.*(..)))"/>
                <aop:around method="myAround" pointcut-ref="pt"/>
                <aop:before method="myBefore" pointcut-ref="pt"/>
                <aop:after method="myAfter" pointcut-ref="pt"/>
    
                <aop:after-returning method="myReturn" pointcut-ref="pt" returning="object"/>
                <aop:after-throwing method="myThrow" pointcut-ref="pt" throwing="e"/>
            </aop:aspect>
        </aop:config>
    </beans>
    
    • aop:point-cut标签指定了切点,返回值、包、类、方法与参数,只匹配符合条件的方法,也就是对这些方法进行代理,条件可以进行与或非操作,分别使用and或&&、or或||、!进行多条件匹配。
    • aop:before标签是前置通知,在切点方法执行前通知
    • aop:around标签是环绕通知,在方法执行前后都通知
    • aop:after标签是后置通知,在方法执行后通知
    • aop:after-returning标签是后置返回通知,方法执行后的返回值进行通知,returning参数所对应的值是方法返回的参数用于放在method方法中的形参。
    • aop:after-throwing是异常抛出通知,只有方法的执行过程中出现了异常并且自己没有处理的话才会执行通知,throwing是捕获到的异常用于放在对应method方法中的形参
    • 执行顺序:before >around before > 业务方法 > (after-throwing)(after returning > around after)>after。不过顺序也不固定:使用注解的话是环绕通知proceed方法之前部分先执行,使用xml配置的话取决于aop:before和aop:around的配置顺序

    MyAspect.java

    package com.qianfeng.aop05;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    import java.util.Arrays;
    
    public class MyAspect{
        public void myBefore(JoinPoint jp){
            System.out.println("args:"+ Arrays.toString(jp.getArgs()));
            System.out.println("toString:"+jp.toString());
            System.out.println("getTarget:"+jp.getTarget());
            System.out.println("---------before----------");
        }
        public void myAfter(){
            System.out.println("---------after----------");
        }
        public Object myAround(ProceedingJoinPoint pjp){
            System.out.println("this is around-before");
            try {
                Object obj = pjp.proceed();
                System.out.println("this is around-after"+obj);
                return obj;
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            return null;
        }
    
        public void myReturn(JoinPoint jp,Object object){
            System.out.println("this is after returning "+object);
        }
    
        public void myThrow(JoinPoint jp,Throwable e){
            System.out.println("this is after throwing "+e.getMessage());
        }
    }
    
    
    • JoinPoint是连接点,会包含连接点的基本消息,通知所在的类、通知的方法名、通知方法的入参集合。getArgs()方法获取方法的参数数组,getTarget()获取目标对象信息
    • ProceedingJoinPoint是JoinPoint的子接口,该对象只用在Around的切面方法中。

    二、第六种方式

    第五种方式使用xml文件配置较多步骤,比较麻烦,第六种方式使用注解更为简便

    beans6.xml

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           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
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.qianfeng.aop06"/>
        <aop:aspectj-autoproxy/>
    </beans>
    

    相较于第五种方式,第六种方式主要的不同就是可以实现自动代理,xml文件的配置有以下不同:

    • 引入了三个命名空间,和context有关
    • 添加了context:component-scan标签,用于扫描组件,base-package指定了需要进行组件扫描的包
    • aop:aspectj-autoproxy标签指定了实现自动代理

    MyAspect.java

    package com.qianfeng.aop06;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    import java.util.Arrays;
    @Component
    @Aspect
    public class MyAspect{
        @Pointcut(value = "execution(boolean com.qianfeng.aop06.*.*(..))")
        public void setAll(){}
        @Before(value = "execution(java.lang.String com.qianfeng.aop06.*.*(..))")
        public void myBefore(JoinPoint jp){
            System.out.println("args:"+ Arrays.toString(jp.getArgs()));
            System.out.println("toString:"+jp.toString());
            System.out.println("getTarget:"+jp.getTarget());
            System.out.println("---------before----------");
        }
        @After("setAll()")
        public void myAfter(){
            System.out.println("---------after----------");
        }
        @Around("setAll()")
        public Object myAround(ProceedingJoinPoint pjp){
            System.out.println("this is around-before");
            try {
                Object obj = pjp.proceed();
                System.out.println("this is around-after"+obj);
                return obj;
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            return null;
        }
        @AfterReturning(value = "setAll()",returning = "object")
        public void myReturn(JoinPoint jp,Object object){
            System.out.println("this is after returning "+object);
        }
        @AfterThrowing(value = "setAll()",throwing = "e")
        public void myThrow(JoinPoint jp,Throwable e){
            System.out.println("this is after throwing "+e.getMessage());
        }
    }
    
    
    • @Component注解标注当前类是一个组件,在扫描时会被扫描进来
    • @Aspect标注当前类是一个切面类
    • @PointCut标注切点,为了避免相同的匹配规则被定义多处,专门定义该方法设置执行的匹配规则,各个方法自行调用即可
    • @Before标注前置通知
    • @After标注后置通知
    • @Around标注环绕通知
    • @AfterReturning标注后置返回通知
    • @AfterThorwing标注抛出异常通知
    • 注解的执行顺序稍有不同:(after throwing)around before>before > 业务方法 > around-after > after >after returning

    三、第七种方式

    第七种方式使用BeanPostProcessor方式实现。该接口我们也叫后置处理器,作用是在Bean对象在实例化和依赖注入完毕后,在显示调用初始化方法的前后添加我们自己的逻辑。注意是Bean实例化完毕后及依赖注入完成后触发的。

    Spring中Bean的运行顺序:

    • Spring IoC容器实例化Bean(注意实例化与初始化的区别,实例化是在内存中开辟空间,初始化是对变量赋值)
    • 调用BeanPostProcesson的postProcessBeforeInitialization方法
    • 调用Bean实例的初始化方法
    • 调用BeanPostProcesson的postProcessAfterInitialization方法

    beans7.xml

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           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
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.qianfeng.aop07"/>
        <bean class="com.qianfeng.aop07.MyBeanPostProcessor"/>
    </beans>
    
    • context:component-scan用于指定组件扫描,base-package指定需要扫描的包,只有打上@component注解的类才会被扫描
    • bean class="com.qianfeng.aop07.MyBeanPostProcessor"指定BeanPostProcessor的Factory hook,让每个bean对象初始化是自动回调该对象中的回调方法

    MyBeanPostProcessor.java

    package com.qianfeng.aop07;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    public class MyBeanPostProcessor implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("this is before "+bean);
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("this is after");
            return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
                    MyAspect ma = new MyAspect();
    
                    ma.before();
    
                    Object obj = method.invoke(bean, args);
    
                    ma.after();
    
                    return obj;
                }
            });
    
        }
    }
    
    
    • 这个类实现了BeanPostProcessor接口,重写了postProcessBeforeInitialization()与postProcessAfterInitialization()方法,这两个方法的第一个参数是目标对象的bean,返回值都是Object类型,因为把bean从IoC容器中取出来还需要放回去,如果返回null的话会报异常
    • postProcessBeforeInitialization()是在初始化方法之前执行,postProcessAfterInitialization()是在初始化方法之后执行
    • 配置了包扫描之后,该类会初始化两个对象EventListenerMethodProcessor和DefaultEventListenerFactory,再外加我们自己的组件对象 , 所以会发现有三个before打印。
    • return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() ...)这条语句的作用就是将代理后的对象放进IoC容器中。

    相关文章

      网友评论

          本文标题:Spring AOP(完结)

          本文链接:https://www.haomeiwen.com/subject/olxjhhtx.html