Spring自动代理机制

       (残梦追月原创,转载请注明)

本文地址:http://www.blogjava.net/cmzy/archive/2008/08/17/222624.html

 

     我们一直使用ProxyFactoryBean来显式的创建AOP代理。但是在很多场合,这种方式将会使编写配置文件的工作量大大增加;由于要从 ProxyFactoryBean获得代理对象,也会使应用和Spring之间的耦合度增加。下面介绍使用Spring提供的自动代理机制来解决这类问 题。

1、使用 BeanNameAutoProxyCreator

       Spring提供的BeanNameAutoProxyCreator类允许我们通过Bean的name属性来指定代理的Bean。它暴露了 java.lang.String[]类型的beanNames和 interceptorNames属性。beanNames可以指定被代理的Bean名字列表,支持“*”通配符,例如“*DAO”表示所有名字以 “DAO”结尾的Bean。interceptorNames指定通知(Advice)列表,或者通知者(Advisor)列表。

        下面通过一个例程来演示如何使用BeanNameAutoProxyCreator。在例子中,有两个Bean:TestBeanA和BeanB,并在 TestMain类中的main方法中调用其MyMethod()方法。自动代理将会在方法调用前自动的执行配置的前置通知,输出提示信息。

新建一个名字为AOP_Test4.10的工程,添加Spring的IoC和AOP库后,新建一aop.test包,再分别创建两个类TestBeanA和BeanB,添加MyMethod()方法,代码如下:

代码     查看源代码 打印
  1. /**  
  2.  *   
  3.  */   
  4. package  aop.test;  
  5. /**  
  6.  * @author zhangyong  
  7.  *   
  8.  */   
  9. public   class  TestBeanA {  
  10.      public   void  MyMethod() {  
  11.           System.out.println(this .getClass().getName()  
  12.                 + ".MyMethod() is run!" );  
  13.      }  
  14. }  


代码     查看源代码 打印
  1. /**  
  2.  *   
  3.  */   
  4. package  aop.test;    
  5. /**  
  6.  * @author zhangyong  
  7.  *   
  8.  */   
  9. public   class  BeanB {      
  10.     public   void  MyMethod() {  
  11.          System.out.println(this .getClass().getName()  
  12.                  + ".MyMethod() is run!" );  
  13.     }  
  14. }  

  再创建前置通知类BeforeAdvice:

代码     查看源代码 打印
  1. /**  
  2.  *   
  3.  */   
  4. package  aop.test;    
  5. import  java.lang.reflect.Method;  
  6. import  org.springframework.aop.MethodBeforeAdvice;    
  7. /**  
  8.  * @author zhangyong  
  9.  *   
  10.  */   
  11. public   class  BeforeAdvice  implements  MethodBeforeAdvice {    
  12.      public   void  before(Method method, Object[] args, Object target)  
  13.              throws  Throwable {  
  14.          System.out.println(method.getName() + "(),将要运行!" );  
  15.      }  
  16. }  

  最后创建含有main方法的测试类TestMain:

代码     查看源代码 打印
  1. /**  
  2.  *   
  3.  */   
  4. package  aop.test;  
  5.    
  6. import  org.springframework.context.ApplicationContext;  
  7. import  org.springframework.context.support.ClassPathXmlApplicationContext;    
  8. /**  
  9.  * @author zhangyong  
  10.  *   
  11.  */   
  12. public   class  TestMain {   
  13.      public   static   void  main(String[] args) {  
  14.          ApplicationContext ac = new  ClassPathXmlApplicationContext(  
  15.                  "applicationContext.xml" );          
  16.          TestBeanA beanA = (TestBeanA)ac.getBean("TestBeanA" );  
  17.          beanA.MyMethod();  
  18.          BeanB beanB = (BeanB)ac.getBean("BeanB" );  
  19.          beanB.MyMethod();          
  20.      }  
  21.  }  

  在配置文件中配置Bean和自动代理Bean,完成后代码如下:

代码     查看源代码 打印
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans  ………… >    
  3.      < bean   id = "TestBeanA"   class = "aop.test.TestBeanA" />   
  4.      < bean   id = "BeanB"   class = "aop.test.BeanB" />     
  5.      < bean   id = "BeforeAdvice"   class = "aop.test.BeforeAdvice" > </ bean >   
  6.     
  7.      < bean   class ="org.springframework.aop.framework.autoproxy.  
  8.                        BeanNameAutoProxyCreator">   
  9.          < property   name = "beanNames" >   
  10.              < list >   
  11.                  < value > Test* </ value >   
  12.              </ list >   
  13.          </ property >   
  14.          < property   name = "interceptorNames" >   
  15.              < list >   
  16.                  < value > BeforeAdvice </ value >   
  17.              </ list >   
  18.          </ property >   
  19.      </ bean >   
  20.  </ beans >   

  运行主类,输出结果如下:

AutoProxy1

  可以看到,在主类TestMain中,我们是直接从Spring IoC容器中获取收管Bean而不是像以前那样从ProxyFactoryBean中获取代理,但是我们的前置通知BeforeAdvice仍然在 TestBeanA对象的MyMethod()方法执行前被触发,这说明我们的自动代理正在工作。

 

2、使用 DefaultAdvisorAutoProxyCreator

  DefaultAdvisorAutoProxyCreator允许我们只需定义相应的Advisor通知者,就可以完成自动代理。配 置好DefaultAdvisorAutoProxyCreator受管Bean后,它会自动查找配置文件中定义的Advisor,并将它们作用于所有的 Bean。

修改例程4.10的配置文件,使用DefaultAdvisorAutoProxyCreator来完成自动代理。完成后配置文件代码如下(本例完整工程代码见例程4.11):

代码     查看源代码 打印
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans  …… >   
  3.      < bean   id = "TestBeanA"   class = "aop.test.TestBeanA"   />   
  4.      < bean   id = "BeanB"   class = "aop.test.BeanB"   />   
  5.      < bean   id = "BeforeAdvice"   class = "aop.test.BeforeAdvice" />   
  6.      < bean   class ="org.springframework.aop.framework.autoproxy.  
  7.                    DefaultAdvisorAutoProxyCreator" />   
  8.     
  9.     < bean   class ="org.springframework.aop.support.NameMatchMethod  
  10.                                           PointcutAdvisor">   
  11.          < property   name = "advice"   ref = "BeforeAdvice"   />   
  12.          < property   name = "mappedNames" >   
  13.              < list >   
  14.                  < value > *Method* </ value >   
  15.              </ list >   
  16.          </ property >   
  17.      </ bean >       
  18.  </ beans >   

  运行主类输出结果如下:

AutoProxy2



By:残梦追月

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值