使用DefaultAdvisorAutoProxyCreator实现spring

系统 1723 0

DefaultAdvisorAutoProxyCreator这个类功能更为强大,这个类的奇妙之处是他实现了BeanProcessor接口,当ApplicationContext读如所有的Bean配置信息后,这个类将扫描上下文,寻找所有的Advistor(一个Advisor是一个切入点和一个通知的组成),将这些Advisor应用到所有符合切入点的Bean中

业务接口:

 

package  StaticAdvisorTest;

public   interface  Shopping  ... {
  
public  String buySomething(String type);
  
public  String buyAnything(String type);
  
public  String sellSomething(String type);
  
public  String sellAnything(String type);

}

 

业务实现类:

 

package  AutoProxyTwo;

public   class  ShoppingImplA  implements  Shopping  ... {
    
private  Customer customer;
    
public  Customer getCustomer()  ... {
        
return  customer;
    }

    
public   void  setCustomer(Customer customer)  ... {
        
this .customer  =  customer;
    }

    
public  String buySomething(String type)  ... {
        System.out.println(
this .getCustomer().getName() + "  bye  " + type + "  success " );
        
return   null ;
    }

    
    
public  String buyAnything(String type)  ... {
       System.out.println(
this .getCustomer().getName() + "  bye  " + type + "  success " );
       
return   null ;

     }

    
public  String sellAnything(String type)  ... {
        System.out.println(
this .getCustomer().getName() + "  sell  " + type + "  success " );
        
return   null ;
    }

    
public  String sellSomething(String type)  ... {
         System.out.println(
this .getCustomer().getName() + "  sell  " + type + "  success " );
           
return   null ;
    }


}



package  AutoProxyTwo;

public   class  ShoppingImplB  implements  Shopping  ... {
    
private  Customer customer;
    
public  Customer getCustomer()  ... {
        
return  customer;
    }

    
public   void  setCustomer(Customer customer)  ... {
        
this .customer  =  customer;
    }

    
public  String buySomething(String type)  ... {
        System.out.println(
this .getCustomer().getName() + "  bye  " + type + "  success " );
        
return   null ;
    }

    
    
public  String buyAnything(String type)  ... {
       System.out.println(
this .getCustomer().getName() + "  bye  " + type + "  success " );
       
return   null ;

     }

    
public  String sellAnything(String type)  ... {
        System.out.println(
this .getCustomer().getName() + "  sell  " + type + "  success " );
        
return   null ;
    }

    
public  String sellSomething(String type)  ... {
         System.out.println(
this .getCustomer().getName() + "  sell  " + type + "  success " );
           
return   null ;
    }


}

 

 通知:

 

package  AutoProxyTwo;

import  java.lang.reflect.Method;

import  org.springframework.aop.MethodBeforeAdvice;
// 前置通知
public   class  WelcomeAdvice  implements  MethodBeforeAdvice  ... {

    
public   void  before(Method method, Object[] args, Object obj)
            
throws  Throwable  ... {
        
        System.out.println(
" Hello welcome to bye  " );

    }


}

 

配置文件:

我们配置一个advisor,方法和在我的blog关于静态切入点的用正则表达式配置切入点相同,这里匹配的是业务实现类中所有型如:***sell***的方法

buyBean和sellBean是最为普通的IOC配置

重点在autoProxyCreator中,我们只需配置一个id和class,spring会自动帮我们解析advisor,并将通知进行切入

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"  >
< beans >
 
< bean  id ="WelcomeAdvice"  class ="AutoProxyTwo.WelcomeAdvice" >
 
</ bean >
 
 
<!--  自动代理所有的advisor  -->
 
< bean  id ="autoProxyCreator"  class ="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >
 
</ bean >
   
   
   
< bean  id ="advisor"  class ="org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
     
< property  name ="pattern" >
       
< value > .*sell.+ </ value >    <!--  业务实现方法名匹配  -->
     
</ property >
     
< property  name ="advice" >
       
< ref  bean ="WelcomeAdvice" />
     
</ property >
   
</ bean >
   
   
  
< bean  id ="buyBean"  class ="AutoProxyTwo.ShoppingImplA" >
    
< property  name ="customer" >
      
< ref  bean ="customer" />
    
</ property >
   
</ bean >
  
< bean  id ="sellBean"  class ="AutoProxyTwo.ShoppingImplB" >
    
< property  name ="customer" >
      
< ref  bean ="customer" />
    
</ property >
   
</ bean >


< bean  id ="customer"  class ="AutoProxyTwo.Customer" >
   
< constructor-arg  index ="0" >
     
< value > gaoxiang </ value >
   
</ constructor-arg >
    
< constructor-arg  index ="1" >
     
< value > 26 </ value >
   
</ constructor-arg >
 
</ bean >


</ beans >

 

测试程序:

需要注意的是,和BeanNameAutoProxyCreator相同,我们需要用ApplicationContext获得Bean

package  AutoProxyTwo;

import  java.io.File;

import  org.springframework.beans.factory.BeanFactory;
import  org.springframework.beans.factory.xml.XmlBeanFactory;
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.FileSystemXmlApplicationContext;
import  org.springframework.core.io.FileSystemResource;
public   class  TestAdvisor  ... {

    
public   static   void  main(String[] args)  ... {

        String filePath
= System.getProperty( " user.dir " ) + File.separator + " AutoProxyTwo " + File.separator + " hello.xml " ;
        
        BeanFactory factory
= new  XmlBeanFactory( new  FileSystemResource(filePath));
        ApplicationContext ctx
= new  FileSystemXmlApplicationContext(filePath);
        Shopping shoppingA
= null ;
        Shopping shoppingB
= null

使用DefaultAdvisorAutoProxyCreator实现spring的自动代理


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论