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

    <bean  />

    <bean  
        class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="target" ref="customerService" />

        <property name="interceptorNames">
            <list>
                <value>customerAdvisor</value>
            </list>
        </property>
    </bean>

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

    <bean  />

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

    <bean  />

    <bean 
        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

相关文章:

  • 2021-10-19
  • 2021-05-07
  • 2021-11-08
  • 2021-08-17
  • 2021-07-11
  • 2022-12-23
  • 2021-12-10
  • 2021-08-13
猜你喜欢
  • 2022-12-23
  • 2021-12-12
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
  • 2022-01-29
  • 2021-07-02
相关资源
相似解决方案