Spring3系列11- Spring AOP——自动创建Proxy
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如下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class=" com.lei.demo.aop.advice.CustomerService"> <property name="name" value="LeiOOLei" /> <property name="url" value="http://www.cnblogs.com/leiOOlei/" /> </bean> <bean id="hijackAroundMethodBean" class=" com.lei.demo.aop.advice.HijackAroundMethod" /> <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" /> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref=" hijackAroundMethodBean " /> </bean> </beans>
配置完后要得到customerServiceProxy,需要如下代码
CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
在自动模式中,你需要创建BeanNameAutoProxyCreator,将所有的bean(通过名字或正则表达式匹配)和advisor形成一个独立的单元,配置如下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"> <property name="name" value="LeiOOLei" /> <property name="url" value="http://www.cnblogs.com/leiOOlei/" /> </bean> <bean id="hijackAroundMethodBeanAdvice" class=" com.lei.demo.aop.advice.HijackAroundMethod" /> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean> </beans>
以上配置中只要bean的id符合*Service,就会自动创建proxy,所以,你可以用以下代码获得proxy。
CustomerService cust = (CustomerService) appContext.getBean("customerService");
2. 利用DefaultAdvisorAutoProxyCreator创建Proxy
这种方式利用DefaultAdvisorAutoProxyCreator实现自动创建Proxy,此种方式威力巨大,任何匹配Advisor的bean,都会自动创建Proxy实现AOP,所以慎用。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"> <property name="name" value="LeiOOLei" /> <property name="url" value="http://www.cnblogs.com/leiOOlei/" /> </bean> <bean id="hijackAroundMethodBeanAdvice" class="com.lei.demo.aop.advice.HijackAroundMethod" /> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> </beans>
以上例子中,xml中任何bean,只要有method名字为printName,使用以下代码时,都会自动创建Proxy,来支持AOP。
CustomerService cust = (CustomerService) appContext.getBean("customerService");