《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");
}
}
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;
}
}
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"));
}
}
}
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
- 《Pro Spring》学习笔记之ComposablePointcut组合切入点实例
- 《Pro Spring》学习笔记之StaticMethodMatcherPointCut静态切入点实例
- 《Pro Spring》学习笔记之DynamicMethodMatcherPointCut动态切入点实例
- 《Pro Spring》学习笔记之JdkRegexpMethodPointCut正则表达式切入点实例
- 《Pro Spring》学习笔记之ControlFlowPointCut流程切入点实例
- 《Pro Spring》学习笔记之NameMatchMethodPointCut方法名匹配切入点实例
- 《pro Spring》学习笔记之Spring集成RMI实例
- 《Pro Spring》学习笔记之Introductions引用实例
- 《pro Spring》学习笔记之Spring+Axis1.4开发WebService实例之复杂类型处理
- 《pro Spring》学习笔记之Spring+Axis1.4开发WebService实例
- 《Pro Spring》学习笔记之集合注入
- 《Pro Spring》学习笔记之依赖解析
- 《Pro Spring》学习笔记之自动装配
- 《Pro Spring》学习笔记之依赖检查
- 《Pro Spring》学习笔记之InitializingBean
- 《Pro Spring》学习笔记之PropertyEditor
- 《Pro Spring》学习笔记之ApplicationContext国际化
- 《Pro Spring》学习笔记之Automatic Proxying自动AOP代理实例
- 实体关联(1)
- 增加无限长精确整数(infinite precision integer)给C++标准库的提案 简单翻译稿 N1692
- 在dataset中添加新行
- java中的浅复制与深复制
- HTML中父节点和子节点
- 《Pro Spring》学习笔记之ComposablePointcut组合切入点实例
- Flash的发展历史
- 太多的细节有问题,可能就是战略上出了问题!
- 斐波那契数的 O(lgn) 时间复杂度算法
- 搜索引擎开发,垂直搜索开发探讨:蜘蛛,并行,搜索,垂直搜索,搜索开发,lucene,java,分布[原创]
- 关于SqlServer Identity列的常用操作
- American Hospitality
- falsh 学习站点
- 号线系统,电讯资源管理:电话号码的节点标识系统[原创/草稿+补充转载]