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

相关文章
相关标签/搜索