Spring5源码(30)-基于Schema的AOP

简介: Spring5源码(30)-基于Schema的AOP


前几篇已经对AOP中的相关概念做了解释,但是都是通过编码方式实现的,每次都需要通过ProxyFactory去创建代理,接下来我们介绍Spring中的自动代理方式来实现AOP,基于Schema配置文件方式和基于@AspectJ注解的方式。当然自动代理实现的机制,放到后面的章节分析,本篇权当温习,也为接下来的源码分析做好铺垫。

1.普通切面
  • 目标对象

package com.lyc.cn.v2.day06;
public interface Animal {
    void sayHello(String name,int age);
    void sayException(String name, int age);
}

package com.lyc.cn.v2.day06;
public class Cat implements Animal {
    @Override
    public void sayHello(String name, int age) {
        System.out.println("--调用被增强方法");
    }
    @Override
    public void sayException(String name, int age) {
        System.out.println("==抛出异常:" + 1 / 0);
    }
}
  • 切面类

package com.lyc.cn.v2.day06;
import org.aspectj.lang.ProceedingJoinPoint;
public class CatAspect {
    /**
     * 前置增强
     */
    public void beforeAdvice(String name, int age) {
        System.out.println("==前置增强,name:" + name + ",age:" + age);
    }
    /**
     * 后置异常增强
     */
    public void afterExceptionAdvice(String name, int age) {
        System.out.println("==后置异常增强,name:" + name + ",age:" + age);
    }
    /**
     * 后置返回增强
     */
    public void afterReturningAdvice(String name, int age) {
        System.out.println("==后置返回增强,name:" + name + ",age:" + age);
    }
    /**
     * 后置最终增强
     */
    public void afterAdvice(String name, int age) {
        System.out.println("==后置最终增强,name:" + name + ",age:" + age);
    }
    /**
     * 环绕增强
     */
    public Object roundAdvice(ProceedingJoinPoint p, String name, int age) {
        System.out.println("==环绕增强开始,name:" + name + ",age:" + age);
        Object o = null;
        try {
            o = p.proceed();
            Object[] args = p.getArgs();
            if (null != args) {
                for (int i = 0; i < args.length; i++) {
                    System.out.println("==环绕增强参数值:" + args[i]);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("==环绕增强结束,name:" + name + ",age:" + age);
        return o;
    }
}
  • 配置文件

<?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="cat" class="com.lyc.cn.v2.day06.Cat"/>
    <!-- 切面类-->
    <bean id="catAspect" class="com.lyc.cn.v2.day06.CatAspect"/>
    <!--AOP配置(一)-->
    <aop:config proxy-target-class="true">
        <!-- 切入点-->
        <aop:pointcut id="pointcut" expression="execution(* com.lyc.cn.v2.day06..*.*(..)) and args(name,age)"/>
        <aop:aspect ref="catAspect" order="0">
            <!--前置增强,在切入点选择的方法之前执行-->
            <aop:before method="beforeAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
            <!--后置异常增强,在切入点选择的方法抛出异常时执行-->
            <aop:after-throwing method="afterExceptionAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
            <!--后置返回增强,在切入点选择的方法正常返回时执行-->
            <aop:after-returning method="afterReturningAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
            <!--后置最终增强,在切入点选择的方法返回时执行,不管是正常返回还是抛出异常都执行-->
            <aop:after method="afterAdvice" pointcut-ref="pointcut" arg-names="name,age"/>
            <!--
                环绕增强,环绕着在切入点选择的连接点处的方法所执行的通知,可以决定目标方法是否执行,
                什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值
            -->
            <aop:around method="roundAdvice" pointcut-ref="pointcut" arg-names="p,name,age"/>
        </aop:aspect>
    </aop:config>
</beans>
  • 测试类及结果

package com.lyc.cn.v2.day06;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    @Test
    public void test1() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("v2/day06.xml");
        Cat cat = ctx.getBean("cat", Cat.class);
        cat.sayHello("美美", 3);
    }
    @Test
    public void test2() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("v2/day06.xml");
        Cat cat = ctx.getBean("cat", Cat.class);
        cat.sayException("美美", 3);
    }
}

// 测试1
==前置增强,name:美美,age:3
==环绕增强开始,name:美美,age:3
--调用被增强方法
==环绕增强参数值:美美
==环绕增强参数值:3
==环绕增强结束,name:美美,age:3
==后置最终增强,name:美美,age:3
==后置返回增强,name:美美,age:3

// 测试2
==前置增强,name:美美,age:3
==环绕增强开始,name:美美,age:3
==环绕增强结束,name:美美,age:3
==后置最终增强,name:美美,age:3
==后置返回增强,name:美美,age:3java.lang.ArithmeticException: / by zero
    at com.lyc.cn.v2.day06.Cat.sayException(Cat.java:12)
    at com.lyc.cn.v2.day06.Cat$$FastClassBySpringCGLIB$$336350b6.invoke(<generated>

相信大家对这样的配置已经非常熟悉了,而且在配置文件中已经有了比较完善的说明,而且Schema的配置方式已经不是那么流行,所以我们不做过多的介绍。

2.引介增强

Spring引入允许为目标类对象引入新的接口。

  • 引介接口和实现

package com.lyc.cn.v2.day06;
/**
 * 引入
 * @author: LiYanChao
 * @create: 2018-10-28 15:48
 */
public interface IIntroduce {
    void sayIntroduce();
}

package com.lyc.cn.v2.day06;
/**
 * @author: LiYanChao
 * @create: 2018-10-28 15:48
 */
public class IntroduceImpl implements IIntroduce {
    @Override
    public void sayIntroduce() {
        System.out.println("--引入");
    }
}
  • 修改配置文件配置引介增强
    <aop:aspect/>标签中加入如下配置:

<!--
    引入
    1、types-matching:匹配需要引入接口的目标对象的AspectJ语法类型表达式。
    2、implement-interface:定义需要引入的接口。
    3、default-impl和delegate-ref:定义引入接口的默认实现,二者选一,
      default-impl是接口的默认实现类全限定名,而delegate-ref是默认的实现的委托Bean名。
-->
<aop:declare-parents types-matching="com.lyc.cn.v2.day06.Cat"
                 implement-interface="com.lyc.cn.v2.day06.IIntroduce"
                 default-impl="com.lyc.cn.v2.day06.IntroduceImpl"/>
  • 测试及结果

@Test
public void test3() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("v2/day06.xml");
    // 注意:getBean获取的是cat
    IIntroduce introduce = ctx.getBean("cat", IIntroduce.class);
    introduce.sayIntroduce();
}

--引入
3.总结

本篇主要回顾一下基于Schema的AOP的配置方式,都是基于配置文件,当然这里涉及到的知识点也很多,这里只是做了简单的介绍,关于更多的配置,大家可以参考Spring的官方文档。





目录
相关文章
|
1月前
|
监控 Java 开发者
Spring AOP动态代理
Spring AOP动态代理
43 1
|
1月前
|
XML 缓存 Java
Spring源码之 Bean 的循环依赖
循环依赖是 Spring 中经典问题之一,那么到底什么是循环依赖?简单说就是对象之间相互引用, 如下图所示: 代码层面上很好理解,在 bean 创建过程中 class A 和 class B 又经历了怎样的过程呢? 可以看出形成了一个闭环,如果想解决这个问题,那么在属性填充时要保证不二次创建 A对象 的步骤,也就是必须保证从容器中能够直接获取到 B。 一、复现循环依赖问题 Spring 中默认允许循环依赖的存在,但在 Spring Boot 2.6.x 版本开始默认禁用了循环依赖 1. 基于xml复现循环依赖 定义实体 Bean java复制代码public class A {
|
1月前
|
Java Spring 容器
Spring的AOP失效场景详解
Spring的AOP失效场景详解
114 0
|
2月前
|
监控 数据可视化 关系型数据库
微服务架构+Java+Spring Cloud +UniApp +MySql智慧工地系统源码
项目管理:项目名称、施工单位名称、项目地址、项目地址、总造价、总面积、施工准可证、开工日期、计划竣工日期、项目状态等。
307 6
|
30天前
|
设计模式 Java Maven
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
35 1
|
2月前
|
Java 关系型数据库 数据库连接
Spring源码解析--深入Spring事务原理
本文将带领大家领略Spring事务的风采,Spring事务是我们在日常开发中经常会遇到的,也是各种大小面试中的高频题,希望通过本文,能让大家对Spring事务有个深入的了解,无论开发还是面试,都不会让Spring事务成为拦路虎。
35 1
|
1月前
|
Java 测试技术 数据库连接
【Spring源码解读!底层原理高级进阶】【下】探寻Spring内部:BeanFactory和ApplicationContext实现原理揭秘✨
【Spring源码解读!底层原理高级进阶】【下】探寻Spring内部:BeanFactory和ApplicationContext实现原理揭秘✨
|
2月前
|
XML Java 数据格式
5个点轻松搞定Spring AOP底层实现原理
AOP 也是 Spring 中一个较为重要的内容,相对于传统的 OOP 模式,AOP 有很多让人难以理解的地方,本篇文章将向大家介绍 AOP 的实现方法及其底层实现,内容包括:
47 1
|
2天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
9天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例