zoukankan      html  css  js  c++  java
  • spring AOP 概述(三) Advisor

    Advisor通知器

      完成对目标方法的切面增强设计(Advice)和关注点的设计(Pointcut)以后,需要一个对象把它们结合起来,完成这个作用的就是Advisor(通知器)。通过Advisor,可以

    定义应该使用哪个通知并在哪个关注点使用它,也就是说通过Advisor,把Advice和Pointcut结合起来。

      我们以一个Advisor的实现(DefaultPointcutAdvisor)为例,来了解Advisor的工作原理,在DefaultPointcutAdvisor中,有两个属性,分别是advice和pointcut

     1 /*
     2  * Copyright 2002-2012 the original author or authors.
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *      http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.
    15  */
    16 
    17 package org.springframework.aop.support;
    18 
    19 import java.io.Serializable;
    20 
    21 import org.aopalliance.aop.Advice;
    22 
    23 import org.springframework.aop.Pointcut;
    24 
    25 /**
    26  * Convenient Pointcut-driven Advisor implementation.
    27  *
    28  * <p>This is the most commonly used Advisor implementation. It can be used
    29  * with any pointcut and advice type, except for introductions. There is
    30  * normally no need to subclass this class, or to implement custom Advisors.
    31  *
    32  * @author Rod Johnson
    33  * @author Juergen Hoeller
    34  * @see #setPointcut
    35  * @see #setAdvice
    36  */
    37 @SuppressWarnings("serial")
    38 public class DefaultPointcutAdvisor extends AbstractGenericPointcutAdvisor implements Serializable {
    39 
    40     private Pointcut pointcut = Pointcut.TRUE;
    41 
    42 
    43     /**
    44      * Create an empty DefaultPointcutAdvisor.
    45      * <p>Advice must be set before use using setter methods.
    46      * Pointcut will normally be set also, but defaults to {@code Pointcut.TRUE}.
    47      */
    48     public DefaultPointcutAdvisor() {
    49     }
    50 
    51     /**
    52      * Create a DefaultPointcutAdvisor that matches all methods.
    53      * <p>{@code Pointcut.TRUE} will be used as Pointcut.
    54      * @param advice the Advice to use
    55      */
    56     public DefaultPointcutAdvisor(Advice advice) {
    57         this(Pointcut.TRUE, advice);
    58     }
    59 
    60     /**
    61      * Create a DefaultPointcutAdvisor, specifying Pointcut and Advice.
    62      * @param pointcut the Pointcut targeting the Advice
    63      * @param advice the Advice to run when Pointcut matches
    64      */
    65     public DefaultPointcutAdvisor(Pointcut pointcut, Advice advice) {
    66         this.pointcut = pointcut;
    67         setAdvice(advice);
    68     }
    69 
    70 
    71     /**
    72      * Specify the pointcut targeting the advice.
    73      * <p>Default is {@code Pointcut.TRUE}.
    74      * @see #setAdvice
    75      */
    76     public void setPointcut(Pointcut pointcut) {
    77         this.pointcut = (pointcut != null ? pointcut : Pointcut.TRUE);
    78     }
    79 
    80     public Pointcut getPointcut() {
    81         return this.pointcut;
    82     }
    83 
    84 
    85     @Override
    86     public String toString() {
    87         return getClass().getName() + ": pointcut [" + getPointcut() + "]; advice [" + getAdvice() + "]";
    88     }
    89 
    90 }
  • 相关阅读:
    每日总结
    每日总结
    Panda Global要点聚焦,中国应加快布局迎接区块链发展升温
    Panda Global热点关注,区块链在物流行业的应用
    Panda 交易所为你解读,区块链提速有望推动数字经济更广阔发展
    Panda 交易所视点观察,金融场景区块链应用进程已经不可逆转
    Panda 交易所焦点观察,区块链技术在各国政府管理中的运用
    Panda 交易所前沿解读,完善顶层设计 构筑区块链创新创业人才高地
    Panda 交易所热点关注,支持区块链技术发展 赋能数字经济
    区块链技术会给我们的生活带来哪些影响?Panda Global 带你一文看懂!
  • 原文地址:https://www.cnblogs.com/toUpdating/p/9763855.html
Copyright © 2011-2022 走看看