概述
StaticMethodMatcherPointcutAdvisor代表一个静态方法匹配切面,它通过StaticMethodMatcherPointcut来定义切点,并通过类过滤和方法名来匹配所定义的切点.
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
我们假设我们业务类中 Waiter和 Seller中都有同名的greetTo()方法.
业务类Waiter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; public class Waiter { /** * * * @Title: greetTo * * @Description: * * @param name * * @return: void */ public void greetTo(String name) { System.out.println( "Waiter Greet to " + name); } /** * * * @Title: serverTo * * @Description: * * @param name * * @return: void */ public void serverTo(String name) { System.out.println( "Waiter Server to " + name); } } |
业务类Seller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; public class Seller { /** * * * @Title: greetTo * * @Description: 和Waiter类中的同名的方法,目的是为了验证仅仅织入了Waiter类中的greetTo方法 * * @param name * * @return: void */ public void greetTo(String name) { System.out.println( "Seller Greet to " + name); } } |
现在我们希望通过StaticMethodMatcherPointcutAdvisor定义一个切面,在Waiter#greetTo()方法调用前织入一个增强,即连接点为Waiter#greetTo()方法调用前的位置。
切面代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; import java.lang.reflect.Method; import org.springframework.aop.ClassFilter; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; /** * * * @ClassName: GreetingAdvisor * * @Description: 切面类 * * @author: Mr.Yang * * @date: 2017年8月18日 下午8:27:52 */ public class GreetingAdvisor extends StaticMethodMatcherPointcutAdvisor { private static final long serialVersionUID = 1L; /** * 重写matches方法,切点方法匹配规则:方法名为greetTo */ @Override public boolean matches(Method method, Class<?> targetClass) { return "greetTo" .equals(method.getName()); } /** * 默认情况下,匹配所有的类,重写getClassFilter,定义匹配规则 切点类型匹配规则,为Waiter的类或者之类 */ public ClassFilter getClassFilter() { return new ClassFilter() { @Override public boolean matches(Class<?> clazz) { return Waiter. class .isAssignableFrom(clazz); } }; } } |
StaticMethodMatcherPointcutAdvisor 抽象类唯一需要定义的是matches()方法,在默认情况下,该切面匹配所有的类,这里通过覆盖getClassFilter()方法,让它仅匹配Waiter类及其子类。
当然,Advisor还需要一个增强类的配合 .
我们来定义一个前置增强
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; /** * * * @ClassName: GreetBeforeAdivce * * @Description: 前置增强 * * @author: Mr.Yang * * @date: 2017年8月18日 下午8:27:40 */ public class GreetBeforeAdivce implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { // 输出切点 System.out.println( "Pointcut:" + target.getClass().getName() + "." + method.getName()); String clientName = (String) args[ 0 ]; System.out.println( "How are you " + clientName + " ?" ); } } |
我们使用Spring配置来定义切面等信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/p" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!-- 配置切面:静态方法匹配切面 --> <!-- Waiter目标类 --> < bean id = "waiterTarget" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Waiter" /> <!-- Seller目标类 --> < bean id = "sellerTarget" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Seller" /> <!-- 前置增强 --> < bean id = "greetBeforeAdvice" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetBeforeAdivce" /> <!-- 切面 --> < bean id = "greetAdvicesor" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetingAdvisor" p:advice-ref = "greetBeforeAdvice" /> <!-- 向切面注入一个前置增强 --> <!-- 通过父bean,配置公共的信息 --> < bean id = "parent" abstract = "true" class = "org.springframework.aop.framework.ProxyFactoryBean" p:interceptorNames = "greetAdvicesor" p:proxyTargetClass = "true" /> <!-- waiter代理 --> < bean id = "waiter" parent = "parent" p:target-ref = "waiterTarget" /> <!-- seller代理 --> < bean id = "seller" parent = "parent" p:target-ref = "sellerTarget" /> </ beans > |
单元测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * * @ClassName: StaticMethodMatcherPointcutAdvisorTest * * @Description: 测试类 * * @author: Mr.Yang * * @date: 2017年8月18日 下午8:29:28 */ public class StaticMethodMatcherPointcutAdvisorTest { @Test public void test() { // 加载配置文件,启动容器 ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:com/xgj/aop/spring/advisor/StaticMethodMatcherPointcutAdvisor/conf-advisor.xml" ); // 从容器中获取Bean Waiter waiter = ctx.getBean( "waiter" , Waiter. class ); Seller seller = ctx.getBean( "seller" , Seller. class ); // 调用业务方法 waiter.greetTo( "XiaoGongJiang" ); waiter.serverTo( "XiaoGongJiang" ); seller.greetTo( "XiaoGongJiang" ); } } |
运行结果:
我们可以看到切面仅仅织入了Wwaiter.greetTo()方法调用前的连接点上, Waiter.serverTo()和Seller.greetTo()方法并没有织入切面。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://artisan.blog.csdn.net/article/details/77393421