• AOP-Advisor-笔记


    一、Advisor接口

    这个接口是一个通知者的顶层接口。它实现类持有一个通知(advice)和一个过滤器的引用。用过滤器来决定通知是否合适目标对象。

    这个接口只有两个方法,所以将整个代码贴上来。

    /**
     * Base interface holding AOP <b>advice</b> (action to take at a joinpoint)
     * and a filter determining the applicability of the advice (such as
     * a pointcut). <i>This interface is not for use by Spring users, but to
     * allow for commonality in support for different types of advice.</i>
     *
     * <p>Spring AOP is based around <b>around advice</b> delivered via method
     * <b>interception</b>, compliant with the AOP Alliance interception API.
     * The Advisor interface allows support for different types of advice,
     * such as <b>before</b> and <b>after</b> advice, which need not be
     * implemented using interception.
     *
     * @author Rod Johnson
     */
    public interface Advisor {
    
        /**
         * Return the advice part of this aspect. An advice may be an
         * interceptor, a before advice, a throws advice, etc.
         * @return the advice that should apply if the pointcut matches
         * @see org.aopalliance.intercept.MethodInterceptor
         * @see BeforeAdvice
         * @see ThrowsAdvice
         * @see AfterReturningAdvice
         *///返回切面中的一个通知,这个通知有可能是个拦截器。
        Advice getAdvice();
    
        /**
         * Return whether this advice is associated with a particular instance
         * (for example, creating a mixin) or shared with all instances of
         * the advised class obtained from the same Spring bean factory.
         * <p><b>Note that this method is not currently used by the framework.</b>
         * Typical Advisor implementations always return {@code true}.
         * Use singleton/prototype bean definitions or appropriate programmatic
         * proxy creation to ensure that Advisors have the correct lifecycle model.
         * @return whether this advice is associated with a particular target instance
         *///返回一个通知是与一个实例关联还是的与多个实例关联,如果只与一个实例关联,那么就返回true.
        boolean isPerInstance();
    
    }

    二、 Advisor的子接口PointcutAdvisor

    这个接口几乎是所有通知者的父接口,除了引入通知者之外。可以在方法级别上判断通知是否适用。

    /**
     * Superinterface for all Advisors that are driven by a pointcut.
     * This covers nearly all advisors except introduction advisors,
     * for which method-level matching doesn't apply.
     *
     * @author Rod Johnson
     */
    public interface PointcutAdvisor extends Advisor {
    
        /**
         * Get the Pointcut that drives this advisor.
         */
        Pointcut getPointcut();
    
    }

     三、AspectJPointcutAdvisor

    一)、这个类的属性

    这个类的属性很少,只有三个。分别是通知、切点、顺序。Advisor就是用来将通知和切点结合在一起的。

        private final AbstractAspectJAdvice advice;//通知
    
        private final Pointcut pointcut;//切点
    
        private Integer order;//顺序,值越小优先级越高。如:值为1的优先级高于值为5的优先级。看Ordered接口。

    二)、这个类的方法

    这个类的方法也很简单,主要是一个构造方法,和若干个setter、getter方法,用来设置和获取以上的三个属性的。

    ①、构造方法

    构造方法初始化通知和切点。

        /**
         * Create a new AspectJPointcutAdvisor for the given advice
         * @param advice the AbstractAspectJAdvice to wrap
         */
        public AspectJPointcutAdvisor(AbstractAspectJAdvice advice) {
            Assert.notNull(advice, "Advice must not be null");
            this.advice = advice;
            this.pointcut = advice.buildSafePointcut();//
        }

    其他setter和getter方法就不列举了,可以想象的到。

    所以现在如果提起Advisor,要能想象到这个Advisor具体是什么样子的。一个Advisor由通知、切点、顺序组成。

    还有一个知识点,Advisor是什么实例化的?

    根据的我的debug,发现它是在SpringIOC容器初始化或者刷新的时候初始化的。具体来说是在AbstractApplicationContext.refresh()方法中。因为Advisor都是非懒加载单例对象。

    refresh()方法中的代码:

                    // Instantiate all remaining (non-lazy-init) singletons.
                    finishBeanFactoryInitialization(beanFactory);

    就是上面这句代码执行了对Advisor的初始化。

  • 相关阅读:
    全球覆盖 哈希
    陌上花开 模板 三维偏序
    洛谷 P4556 [Vani有约会]雨天的尾巴 /【模板】线段树合并
    熟练剖分(tree) 树形DP
    那一天她离我而去 二进制分组建图
    平凡的函数 线性筛积性函数
    wmz的数数(数状数组)
    跳一跳 概率与期望
    洛谷 P4284 [SHOI2014]概率充电器 概率与期望+换根DP
    SpringBoot手动事务参考链接
  • 原文地址:https://www.cnblogs.com/GooPolaris/p/8213580.html
Copyright © 2020-2023  润新知