在spring中与通知相关的类有:

以Advice结尾的通知接口

     MethodBeforeAdvice    AfterReturningAdvice   ThrowsAdvice

以Interceptor结尾的拦截器

MethodBeforeAdviceInterceptor   AfterReturningAdviceInterceptor   ThrowsAdviceInterceptor

以Adapter结尾的适配器

MethodBeforeAdviceAdapter   AfterReturningAdviceAdapter  ThrowsAdviceAdapter

先了解一下Adapter模式

而这三者间的关系以MethodBefore为例:

下面以MethodBefore为例,看一下源码。

先看一下AdvisorAdapter接口

public interface AdvisorAdapter {

    boolean supportsAdvice(Advice advice);

    MethodInterceptor getInterceptor(Advisor advisor);

}

MethodBeforeAdviceAdapter类

class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {

    @Override
public boolean supportsAdvice(Advice advice) {
return (advice instanceof MethodBeforeAdvice);
} @Override
public MethodInterceptor getInterceptor(Advisor advisor) {
MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
return new MethodBeforeAdviceInterceptor(advice);
} }

而MethodBeforeAdviceInterceptor是具体的实现。

例外在看一下MethodBeforeAdviceInterceptor类

public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {

    private MethodBeforeAdvice advice;

    /**
* Create a new MethodBeforeAdviceInterceptor for the given advice.
* @param advice the MethodBeforeAdvice to wrap
*/
public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
Assert.notNull(advice, "Advice must not be null");
this.advice = advice;
} @Override
public Object invoke(MethodInvocation mi) throws Throwable {
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );
return mi.proceed();
} }

实现了MethodIntercepter接口,在调用invoke进行拦截的时候,是先调用before方法里面,然后在调用具体的方法实现。

Spring源码解析-Advice中的Adapter模式的更多相关文章

  1. spring 源码解析

    1. [文件] spring源码.txt ~ 15B     下载(167) ? 1 springн┤┬вио╬Ш: 2. [文件] spring源码分析之AOP.txt ~ 15KB     下载( ...

  2. Spring源码解析——循环依赖的解决方案

    一.前言 承接<Spring源码解析--创建bean>.<Spring源码解析--创建bean的实例>,我们今天接着聊聊,循环依赖的解决方案,即创建bean的ObjectFac ...

  3. Spring源码解析 - AbstractBeanFactory 实现接口与父类分析

    我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ...

  4. Spring源码解析-ioc容器的设计

    Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ...

  5. Spring源码解析系列汇总

    相信我,你会收藏这篇文章的 本篇文章是这段时间撸出来的Spring源码解析系列文章的汇总,总共包含以下专题.喜欢的同学可以收藏起来以备不时之需 SpringIOC源码解析(上) 本篇文章搭建了IOC源 ...

  6. Spring源码解析之BeanFactoryPostProcessor(三)

    在上一章中笔者介绍了refresh()的<1>处是如何获取beanFactory对象,下面我们要来学习refresh()方法的<2>处是如何调用invokeBeanFactor ...

  7. Spring源码解析之ConfigurationClassPostProcessor(二)

    上一个章节,笔者向大家介绍了spring是如何来过滤配置类的,下面我们来看看在过滤出配置类后,spring是如何来解析配置类的.首先过滤出来的配置类会存放在configCandidates列表, 在代 ...

  8. Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean

    Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean 七千字长文深刻解读,Spirng中是如何初始化单例bean的,和面试中最常问的Sprin ...

  9. Spring源码解析之PropertyPlaceholderHelper(占位符解析器)

    Spring源码解析之PropertyPlaceholderHelper(占位符解析器) https://blog.csdn.net/weixin_39471249/article/details/7 ...

随机推荐

  1. 加油吧 骚年QAQ

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2017 年 11 月 08 日 原地址:https://niaobulashi.com/archives/fighting.html --- 想 ...

  2. Python全栈 MongoDB 数据库(Mongo、 正则基础、一篇通)

                  终端命令:       在线安装:         sudo apt-get install mongodb         默认安装路径 :  /var/lib/mong ...

  3. loadrunner_遇到cookie接口_3种应对方法

    方法一:是调用登录接口,在调用登录后的接口 方法二:手动储存cookie,写死cookie 方法一:提前登录收集cookie,写成参数化文件 方法一,案例(就是先登录,再写登录后的接口): 注:use ...

  4. 在本地电脑使用远程服务器的图形界面——包括 MATLAB、PyCharm 等各种软件

    在用本地电脑连接远程服务器的时候,大部分时候只能用命令行来操作.虽然可以 在本地电脑用 PyCharm 进行远程调试.在本地电脑远程使用服务器的 Jupyter Notebook.Ubuntu 和 W ...

  5. [mongodb]child process failed, exited with error number 100

    Run the following command first to start the mongo server mongod run --config /usr/local/etc/mongod. ...

  6. ptrdiff_t类型

    一.特性 1. 这是一种标准库类型 2. 是两个指针相减的结果的类型(因为差值可能为负值,所以是一种带符号类型) 3. 和size_t一样,ptrdiff_t也是一种定义在<cstddef> ...

  7. 软工1816 · Alpha冲刺(2/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 与前后端敲定接口设计的细节 重新理清业务逻辑,对项目最初的设想进行一定修正 跟 ...

  8. HttpServletRequest和HttpServletResponse实例

    先看一下web.xml文件配置: <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...

  9. Swift-map()跟flatMap()区别

    map()方法介绍 map() 是  Array 提供的方法,通过接收一个函数作为传入参数,对数组中每个元素进行函数变换得到新的结果值.这样只需要提供  X->Y 的映射关系,就能将数组  [X ...

  10. 重新看《JavaScript高级程序设计》

    几点心得: 1)数据是基础,一共有3种基础数据:null.undefined.和object:遵循从无到有从简单到复杂的演变过程 2)衍生数据:衍生数据是指操作符合语句,这些是基础数据产生导致的必然结 ...