Spring3系列: Spring AOP——自动创建Proxy

Bertha 。 2022-07-13 12:22 258阅读 0赞

 为尊重别人的劳动成果,本文转自:Spring3系列11- Spring AOP——自动创建Proxy

在《Spring3系列9- Spring AOP——Advice》和《Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法》中的例子中,在配置文件中,你必须手动为每一个需要AOP的bean创建Proxy bean(ProxyFactoryBean)。

这不是一个好的体验,例如,你想让DAO层的所有bean都支持AOP,以便写SQL日志,那么你必须手工创建很多的ProxyFactoryBean,这样会直接导致你的xml配置文件内容成几何级的倍增,不利于xml配置维护。

幸运的是,Spring有两种方法,可以为你自动创建proxy。

1. 利用BeanNameAutoProxyCreator自动创建proxy

手工创建ProxyFactoryBean如下:

复制代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerService" class=" com.lei.demo.aop.advice.CustomerService">
  6. <property name="name" value="LeiOOLei" />
  7. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  8. </bean>
  9. <bean id="hijackAroundMethodBean" class=" com.lei.demo.aop.advice.HijackAroundMethod" />
  10. <bean id="customerServiceProxy"
  11. class="org.springframework.aop.framework.ProxyFactoryBean">
  12. <property name="target" ref="customerService" />
  13. <property name="interceptorNames">
  14. <list>
  15. <value>customerAdvisor</value>
  16. </list>
  17. </property>
  18. </bean>
  19. <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  20. <property name="mappedName" value="printName" />
  21. <property name="advice" ref=" hijackAroundMethodBean " />
  22. </bean>
  23. </beans>

复制代码

配置完后要得到customerServiceProxy,需要如下代码

CustomerService cust = (CustomerService) appContext.getBean(“customerServiceProxy”);

在自动模式中,你需要创建BeanNameAutoProxyCreator,将所有的bean(通过名字或正则表达式匹配)和advisor形成一个独立的单元,配置如下:

复制代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  6. <property name="name" value="LeiOOLei" />
  7. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  8. </bean>
  9. <bean id="hijackAroundMethodBeanAdvice" class=" com.lei.demo.aop.advice.HijackAroundMethod" />
  10. <bean
  11. class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  12. <property name="beanNames">
  13. <list>
  14. <value>*Service</value>
  15. </list>
  16. </property>
  17. <property name="interceptorNames">
  18. <list>
  19. <value>customerAdvisor</value>
  20. </list>
  21. </property>
  22. </bean>
  23. <bean id="customerAdvisor"
  24. class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  25. <property name="mappedName" value="printName" />
  26. <property name="advice" ref="hijackAroundMethodBeanAdvice" />
  27. </bean>
  28. </beans>

复制代码

以上配置中只要bean的id符合*Service,就会自动创建proxy,所以,你可以用以下代码获得proxy。

CustomerService cust = (CustomerService) appContext.getBean(“customerService”);

2. 利用DefaultAdvisorAutoProxyCreator创建Proxy

这种方式利用DefaultAdvisorAutoProxyCreator实现自动创建Proxy,此种方式威力巨大,任何匹配Advisor的bean,都会自动创建Proxy实现AOP,所以慎用。

复制代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  6. <property name="name" value="LeiOOLei" />
  7. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  8. </bean>
  9. <bean id="hijackAroundMethodBeanAdvice" class="com.lei.demo.aop.advice.HijackAroundMethod" />
  10. <bean id="customerAdvisor"
  11. class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  12. <property name="mappedName" value="printName" />
  13. <property name="advice" ref="hijackAroundMethodBeanAdvice" />
  14. </bean>
  15. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
  16. </beans>

复制代码

以上例子中,xml中任何bean,只要有method名字为printName,使用以下代码时,都会自动创建Proxy,来支持AOP。

CustomerService cust = (CustomerService) appContext.getBean(“customerService”);

发表评论

表情:
评论列表 (有 0 条评论,258人围观)

还没有评论,来说两句吧...

相关阅读