zoukankan      html  css  js  c++  java
  • 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");

     

  • 相关阅读:
    避免文本字体大小重置
    为webapp应用制定IOS,Android桌面快捷图标
    兄弟节点 疑问的
    节点属性
    区别getElementByID,getElementsByName,getElementsByTagName
    三种快排四种优化(转载)
    快排(模板)
    二分法求解最大值或最小值(模板)
    中国剩余定理(转载)(中国剩余定理与扩展欧几里德的联系)
    简单母函数(转载)
  • 原文地址:https://www.cnblogs.com/leiOOlei/p/3557964.html
Copyright © 2011-2022 走看看