音乐 关注 标签
评论 参与 新评

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

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

 

XML

使用代理名称“customerServiceProxy”来获得 bean。

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

在自动代理机制,只需要创建一个的 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.yiibai.customer.services.CustomerService">
        <property name="name" value="Yiibai Mook Kim" />
        <property name="url" value="http://www.yiibai.com" />
    </bean>

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

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

 

XML

现在,可以通过“CustomerService”的原始名称获取bean, 如果知道这个bean已经代理。

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

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

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

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

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

</beans>

 

XML

不用管使用什么代理方法, Spring 都会有最适合处理方式。

下载代码 – http://pan.baidu.com/s/1pKdqtjt

posted on 2017-10-26 14:16  ☠☚㊙☛☠  阅读(154)  评论(0编辑  收藏  举报

Top
打赏

打赏

公众号