Spring 专题
专题目录
您的位置:java > Spring专题 > Spring自动代理创建者实例
Spring自动代理创建者实例
作者:--    发布时间:2019-11-20

在上一篇 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.h3.customer.services.customerservice">
        <property name="name" value="h3 mook kim" />
        <property name="url" value="http://www.h3.com" />
    </bean>

    <bean id="hijackaroundmethodbeanadvice" class="com.h3.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.namematchmethodh3cutadvisor">
        <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.h3.customer.services.customerservice">
        <property name="name" value="h3 mook kim" />
        <property name="url" value="http://www.h3.com" />
    </bean>

    <bean id="hijackaroundmethodbeanadvice" class="com.h3.aop.hijackaroundmethod" />

    <bean id="customeradvisor"
        class="org.springframework.aop.support.namematchmethodh3cutadvisor">
        <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.h3.customer.services.customerservice">
        <property name="name" value="h3 mook kim" />
        <property name="url" value="http://www.h3.com" />
    </bean>

    <bean id="hijackaroundmethodbeanadvice" class="com.h3.aop.hijackaroundmethod" />

    <bean id="customeradvisor"
        class="org.springframework.aop.support.namematchmethodh3cutadvisor">
        <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


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册