本篇内容主要讲解“Spring-AOP 静态普通方法名匹配切面的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring-AOP 静态普通方法名匹配切面的方法”吧!
概述
StaticMethodMatcherPointcutAdvisor代表一个静态方法匹配切面,它通过StaticMethodMatcherPointcut来定义切点,并通过类过滤和方法名来匹配所定义的切点.
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
我们假设我们业务类中 Waiter和 Seller中都有同名的greetTo()方法.
业务类Waiter
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor;public class Waiter {public void greetTo(String name) {System.out.println("Waiter Greet to " + name);}public void serverTo(String name) {System.out.println("Waiter Server to " + name);}}
业务类Seller
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor;public class Seller { public void greetTo(String name) { System.out.println("Seller Greet to " + name); }}
现在我们希望通过StaticMethodMatcherPointcutAdvisor定义一个切面,在Waiter#greetTo()方法调用前织入一个增强,即连接点为Waiter#greetTo()方法调用前的位置。
切面代码
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor;import java.lang.reflect.Method;import org.springframework.aop.ClassFilter;import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;public class GreetingAdvisor extends StaticMethodMatcherPointcutAdvisor {private static final long serialVersionUID = 1L;@Overridepublic boolean matches(Method method, Class<?> targetClass) {return "greetTo".equals(method.getName());}public ClassFilter getClassFilter() {return new ClassFilter() {@Overridepublic boolean matches(Class<?> clazz) {return Waiter.class.isAssignableFrom(clazz);}};}}
StaticMethodMatcherPointcutAdvisor 抽象类唯一需要定义的是matches()方法,在默认情况下,该切面匹配所有的类,这里通过覆盖getClassFilter()方法,让它仅匹配Waiter类及其子类。
当然,Advisor还需要一个增强类的配合 .
我们来定义一个前置增强
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class GreetBeforeAdivce implements MethodBeforeAdvice {@Overridepublic 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配置来定义切面等信息
<?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>
单元测试类
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class StaticMethodMatcherPointcutAdvisorTest {@Testpublic void test() {// 加载配置文件,启动容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/StaticMethodMatcherPointcutAdvisor/conf-advisor.xml");// 从容器中获取BeanWaiter 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()方法并没有织入切面。
到此,相信大家对“Spring-AOP 静态普通方法名匹配切面的方法”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!