《Pro Spring》学习笔记之ComposablePointcut组合切入点实例

来源:互联网 发布:mac极品飞车 编辑:程序博客网 时间:2024/05/29 14:17

每一个advisor中一般只定义一个切入点,如果有这样的需求,我们相对一个类中的getter和setter方法都进行通知,一般来说只能定义两个切入点来实现,但没有一个切入点可以同时处理两个,当然,我们可以利用新的逻辑创建一个切入点,但更好的方式是使用ComposablePointcut组合切入点,利用他的切入点union(交集)和intersection(并集)的特性组合两个切入点

union和 intersection方法的参数是ClassFilter和MethodMatcher参数

 

调用接受MethodMathcher的union()方法将ComposablePointCut的MethodMatcher改为一个UnionMethodMathcer,他包含现有的MethodMatcher和union()的MethodMatcher的并集,当任何一个返回true时候,则UnionMethodMathcer返回true,参数ClassFilter也会生成类似的结构

intersection原理和union类似,只是当两个MethodMathcer全部为true方返回true

 

BeanOne.java

 

package ch6.SimpleAOP.MethodMatchNamePointCut;

public class BeanOne {
   
public void foo(){
       System.out.println(
"foo-no-arg");
   }

   
public void foo(int a){
       System.out.println(
"foo-with-arg");
   }

}

 

SimpleAdvise.java

 

package ch6.SimpleAOP.MethodMatchNamePointCut;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class SimpleAdvise implements MethodInterceptor {

    
public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println(
"before");
        Object retVal
=invocation.proceed();
        System.out.println(
"after");
        
return retVal;
    }


}

 

测试

 

package ch7.ComposablePointCut;

import java.lang.reflect.Method;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;

public class Test {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
         BeanOne one
=new BeanOne();
        
         BeanOne proxyOne;
    
         
//FooMethodMatcher和BarMethodMatcher两个切入点的并集    
         ComposablePointcut pc=new ComposablePointcut(ClassFilter.TRUE,new FooMethodMatcher());
         pc.union(
new BarMethodMatcher());
         Advice advice
=new SimpleAdvise();
         Advisor advisor
=new DefaultPointcutAdvisor(pc,advice);
         
         
//FooMethodMatcher和BarMethodMatcher两个切入点的交集
         ComposablePointcut pc1=new ComposablePointcut(ClassFilter.TRUE,new FooMethodMatcher());
         pc1.intersection(
new BarMethodMatcher());
         Advice advice1
=new SimpleAdvise();
         Advisor advisor1
=new DefaultPointcutAdvisor(pc1,advice1);
         
         
//创建BeanOne代理(union)
         ProxyFactory pf1=new ProxyFactory();
         pf1.addAdvisor(advisor);
         pf1.setTarget(one);
         proxyOne
=(BeanOne)pf1.getProxy();
         
         
         
//直接调用foo(union)
         proxyOne.foo();
         proxyOne.bar();
         
         System.out.println(
"-------------------------");
         
         
//创建BeanOne代理(intersection)
         ProxyFactory pf2=new ProxyFactory();
         pf2.addAdvisor(advisor1);
         pf2.setTarget(one);
         proxyOne
=(BeanOne)pf2.getProxy();
         
         
//直接调用foo(intersection)
         proxyOne.foo();
         proxyOne.bar();
        
         
         
    }

    
    
    
//自定义MethodMather类,匹配所有foo开头的方法
    private  static class FooMethodMatcher extends StaticMethodMatcher{
        
public boolean matches(Method method,Class cls){
            
return (method.getName().startsWith("foo"));
        }

        
    }

//    自定义MethodMather类,匹配所有bar开头的方法
    private  static class BarMethodMatcher extends StaticMethodMatcher{
        
public boolean matches(Method method,Class cls){
            
return (method.getName().equals("bar"));
        }

        
    }

    


}

 

运行结果:

before
foo-one
after
before
bar-one
after
-------------------------
foo-one
bar-one