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 }

猜你喜欢

转载自www.cnblogs.com/toUpdating/p/9763855.html