Spring中创建自己的PointcutAdvisor
在Spring中使用的Advisor中,大部分情况下使用的都是容器已经定义的PointcutAdvisor,当然如果我们实现指定的接口,也可以将自定义的PointcutAdvisor。
首先,要定义一个Advice,我们自定义一个RegardCustomer实现MethodBeforeAdvice,当然也可以实现其他的Advice接口:
java 代码
- import java.lang.reflect.Method;
- import org.springframework.aop.MethodBeforeAdvice;
- import org.springframework.aop.AfterReturningAdvice;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- import org.springframework.aop.ThrowsAdvice;
- import org.springframework.aop.support.RegexpMethodPointcutAdvisor;
- public class RegardCustomer implements MethodBeforeAdvice
- {
- public void before(Method method, Object[] args, Object target)
- {
- System.out.println("hi");
- }
- }
然后,自定义一个类实现ClassFilter接口,这个接口实现对调用方法的类进行匹配,如果你想只通过方法名称来匹配,就让matches方法返回true即可,如下:
java 代码
- import org.springframework.aop.*;
- public class MyClassFilter implements ClassFilter
- {
- public boolean matches(Class clazz)
- {
- return clazz.equals(EnterShopIml.class);
- }
- }
然后,自定义一个类实现MethodMather接口来进行对方法名称的匹配:
java 代码
- import org.springframework.aop.*;
- import java.lang.reflect.*;
- public class MyMethodMatcher implements MethodMatcher
- {
- public boolean matches(Method m, Class targetClass)
- {
- return m.getName().equals("buy");
- }
- public boolean isRuntime()
- {
- return false;
- }
- public boolean matches(Method m, Class target, Object[] args)
- {
- return true;
- }
- }
现在,我们让方法名称是buy时匹配返回true,另外,我们定义的是静态的方法匹配,让isRuntime()放回false,这样,上面JAVA文件中的第3个方法永远不会被调用。
然后,要有一个类实现Pointcut接口,如下:
java 代码
- import org.springframework.aop.*;
- public class MyPointcut implements Pointcut
- {
- public ClassFilter getClassFilter()
- {
- return new MyClassFilter();
- }
- public MethodMatcher getMethodMatcher()
- {
- return new MyMethodMatcher();
- }
- }
然后,要把我们的advice和pointcut给结合到一起,所以要有一个类实现PointcutAdvisor,如下:
java 代码
- import org.springframework.aop.*;
- import org.aopalliance.aop.*;;
- public class MyPointcutAdvisor implements PointcutAdvisor
- {
- public Pointcut getPointcut()
- {
- return new MyPointcut();
- }
- public Advice getAdvice()
- {
- return new RegardCustomer();
- }
- public boolean isPerInstance()
- {
- return true;
- }
- }
下面是一个自定义接口和类
java 代码
- public interface EnterShop
- {
- public abstract void buy() throws Exception;
- }
java 代码
- public class EnterShopIml implements EnterShop
- {
- public void buy() throws Exception
- {
- System.out.println("buy()");
- }
- }
然后就是,Spring的配置文件:
xml 代码
- xml version="1.0" encoding="UTF-8"?>
- "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="enterShopTarget"
- class="EnterShopIml"/>
- <bean id="regardAdvisor"
- class="MyPointcutAdvisor">
- bean>
- <bean id="enterShop"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">
- <value>EnterShopvalue>
- property>
- <property name="interceptorNames">
- <list>
- <value>regardAdvisorvalue>
- list>
- property>
- <property name="target">
- <ref bean="enterShopTarget"/>
- property>
- bean>
- beans>