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()方法,代码如下:

代码 查看源代码 copy to clipboard 打印
  1. /**

  2. *

  3. */

  4. package aop.test;  

  5. /**

  6. * @author zhangyong

  7. *

  8. */

  9. publicclass TestBeanA {  

  10. publicvoid MyMethod() {  

  11.          System.out.println(this.getClass().getName()  

  12.                + ".MyMethod() is run!");  

  13.     }  

  14. }  

/** * */ package aop.test; /** * @author zhangyong * */ public class TestBeanA { public void MyMethod() { System.out.println(this.getClass().getName() + ".MyMethod() is run!"); } }

代码 查看源代码 copy to clipboard 打印
  1. /**

  2. *

  3. */

  4. package aop.test;    

  5. /**

  6. * @author zhangyong

  7. *

  8. */

  9. publicclass BeanB {      

  10. publicvoid MyMethod() {  

  11.         System.out.println(this.getClass().getName()  

  12.                 + ".MyMethod() is run!");  

  13.    }  

  14. }  

/** * */ package aop.test; /** * @author zhangyong * */ public class BeanB { public void MyMethod() { System.out.println(this.getClass().getName() + ".MyMethod() is run!"); } }

  再创建前置通知类BeforeAdvice:

代码 查看源代码 copy to clipboard 打印
  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. publicclass BeforeAdvice implements MethodBeforeAdvice {    

  12. publicvoid before(Method method, Object[] args, Object target)  

  13. throws Throwable {  

  14.         System.out.println(method.getName() + "(),将要运行!");  

  15.     }  

  16. }  

/** * */ package aop.test; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; /** * @author zhangyong * */ public class BeforeAdvice implements MethodBeforeAdvice { public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(method.getName() + "(),将要运行!"); } }

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

代码 查看源代码 copy to clipboard 打印
  1. /**

  2. *

  3. */

  4. package aop.test;  


  5. import org.springframework.context.ApplicationContext;  

  6. import org.springframework.context.support.ClassPathXmlApplicationContext;    

  7. /**

  8. * @author zhangyong

  9. *

  10. */

  11. publicclass TestMain {  

  12. publicstaticvoid main(String[] args) {  

  13.         ApplicationContext ac = new ClassPathXmlApplicationContext(  

  14. "applicationContext.xml");          

  15.         TestBeanA beanA = (TestBeanA)ac.getBean("TestBeanA");  

  16.         beanA.MyMethod();  

  17.         BeanB beanB = (BeanB)ac.getBean("BeanB");  

  18.         beanB.MyMethod();          

  19.     }  

  20. }  

/** * */ package aop.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author zhangyong * */ public class TestMain { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml"); TestBeanA beanA = (TestBeanA)ac.getBean("TestBeanA"); beanA.MyMethod(); BeanB beanB = (BeanB)ac.getBean("BeanB"); beanB.MyMethod(); } }

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

代码 查看源代码 copy to clipboard 打印
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <beans …………>

  3. <beanid="TestBeanA"class="aop.test.TestBeanA"/>

  4. <beanid="BeanB"class="aop.test.BeanB"/>

  5. <beanid="BeforeAdvice"class="aop.test.BeforeAdvice"></bean>


  6. <beanclass="org.springframework.aop.framework.autoproxy.  

  7.                       BeanNameAutoProxyCreator">

  8. <propertyname="beanNames">

  9. <list>

  10. <value>Test*</value>

  11. </list>

  12. </property>

  13. <propertyname="interceptorNames">

  14. <list>

  15. <value>BeforeAdvice</value>

  16. </list>

  17. </property>

  18. </bean>

  19. </beans>

<?xml version="1.0" encoding="UTF-8"?> <beans …………> <bean id="TestBeanA" class="aop.test.TestBeanA"/> <bean id="BeanB" class="aop.test.BeanB"/> <bean id="BeforeAdvice" class="aop.test.BeforeAdvice"></bean> <bean class="org.springframework.aop.framework.autoproxy. BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>Test*</value> </list> </property> <property name="interceptorNames"> <list> <value>BeforeAdvice</value> </list> </property> </bean> </beans>

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

AutoProxy1

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


2、使用DefaultAdvisorAutoProxyCreator

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

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

代码 查看源代码 copy to clipboard 打印
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <beans ……>

  3. <beanid="TestBeanA"class="aop.test.TestBeanA"/>

  4. <beanid="BeanB"class="aop.test.BeanB"/>

  5. <beanid="BeforeAdvice"class="aop.test.BeforeAdvice"/>

  6. <beanclass="org.springframework.aop.framework.autoproxy.  

  7.                   DefaultAdvisorAutoProxyCreator" />


  8. <beanclass="org.springframework.aop.support.NameMatchMethod  

  9.                                          PointcutAdvisor">

  10. <propertyname="advice"ref="BeforeAdvice"/>

  11. <propertyname="mappedNames">

  12. <list>

  13. <value>*Method*</value>

  14. </list>

  15. </property>

  16. </bean>

  17. </beans>

<?xml version="1.0" encoding="UTF-8"?> <beans ……> <bean id="TestBeanA" class="aop.test.TestBeanA" /> <bean id="BeanB" class="aop.test.BeanB" /> <bean id="BeforeAdvice" class="aop.test.BeforeAdvice"/> <bean class="org.springframework.aop.framework.autoproxy. DefaultAdvisorAutoProxyCreator" /> <bean class="org.springframework.aop.support.NameMatchMethod PointcutAdvisor"> <property name="advice" ref="BeforeAdvice" /> <property name="mappedNames"> <list> <value>*Method*</value> </list> </property> </bean> </beans>

  运行主类输出结果如下:

AutoProxy2


你可能感兴趣的:(spring自动代理例子)