《Pro Spring》学习笔记之Advisor的便利实现

Spring为我们提供了诸如NameMatchMethodPointcutAdvisor的类,这个类可以同时充当PointCut和Advisor

看如下代码:

我们不需要额外的创建PointCut实例就可以完成切面织入

package  ch6.SimpleAOP.PointCutAdvisor;

import  org.aopalliance.aop.Advice;
import  org.springframework.aop.Advisor;
import  org.springframework.aop.Pointcut;
import  org.springframework.aop.framework.ProxyFactory;
import  org.springframework.aop.support.DefaultPointcutAdvisor;
import  org.springframework.aop.support.NameMatchMethodPointcutAdvisor;

public   class  Test  {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
         BeanOne one
=new BeanOne();
    
         BeanOne proxyOne;
        
         
        NameMatchMethodPointcutAdvisor advisor
=new NameMatchMethodPointcutAdvisor(new SimpleAdvise());
        advisor.addMethodName(
"foo");
        
         
//创建BeanOne代理
         ProxyFactory pf1=new ProxyFactory();
         pf1.addAdvisor(advisor);
         pf1.setTarget(one);
         proxyOne
=(BeanOne)pf1.getProxy();
    
         
         proxyOne.foo();
         proxyOne.bar();
         
        
         
    }


}

 这个案例针对方法名匹配的切入点,其他通知者的实现类请参考Spring DOC

你可能感兴趣的:(《Pro Spring》学习笔记之Advisor的便利实现)