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)。html

這不是一個好的體驗,例如,你想讓DAO層的全部bean都支持AOP,以便寫SQL日誌,那麼你必須手工建立不少的ProxyFactoryBean,這樣會直接致使你的xml配置文件內容成幾何級的倍增,不利於xml配置維護。正則表達式

幸運的是,Spring有兩種方法,能夠爲你自動建立proxy。spring

 

1.       利用BeanNameAutoProxyCreator自動建立proxy

手工建立ProxyFactoryBean以下:app

<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,須要以下代碼url

CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");spa

 

在自動模式中,你須要建立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。code

CustomerService cust = (CustomerService) appContext.getBean("customerService");xml

 

2.      利用DefaultAdvisorAutoProxyCreator建立Proxy

這種方式利用DefaultAdvisorAutoProxyCreator實現自動建立Proxy,此種方式威力巨大,任何匹配Advisor的bean,都會自動建立Proxy實現AOP,因此慎用。htm

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

相關文章
相關標籤/搜索