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");