AOP - 自动创建 Proxy

本文介绍了Spring AOP中自动创建Proxy的三种方法,包括利用BeanNameAutoProxyCreator、DefaultAdvisorAutoProxyCreator等工具类简化Proxy创建过程,从而提高开发效率。
部署运行你感兴趣的模型镜像

知识点

  • 自动创建Proxy

项目文件结构

这里写图片描述

三、实验步骤

3.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.xsd">

    <bean id="customerService" class=" com.shiyanlou.spring.aop.advice.CustomerService">
        <property name="name" value="Shiyanlou" />
        <property name="url" value="shiyanlou.com" />
    </bean>

    <bean id="hijackAroundMethodBean" class=" com.shiyanlou.spring.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.xsd">

    <bean id="customerService" class=" com.shiyanlou.spring.aop.advice.CustomerService">
        <property name="name" value="Shiyanlou" />
        <property name="url" value="shiyanlou.com" />
    </bean>

    <bean id="hijackAroundMethodBean" class=" com.shiyanlou.spring.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="hijackAroundMethodBean" />
</bean>

</beans>

以上配置中只要 bean 的 id 符合 *Service ,就会自动创建 proxy ,所以,可以用以下代码获得 proxy 。

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

运行结果如下:

这里写图片描述

3.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.xsd">

    <bean id="customerService" class=" com.shiyanlou.spring.aop.advice.CustomerService">
        <property name="name" value="Shiyanlou" />
        <property name="url" value="shiyanlou.com" />
    </bean>

    <bean id="hijackAroundMethodBean" class=" com.shiyanlou.spring.aop.advice.HijackAroundMethod" />

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

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

以上例子中,xml 中任何 bean ,只要有 method 名字为 printName ,使用以下代码时,都会自动创建 Proxy ,来支持 AOP 。

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

实验结果同上。

总结

我们学习了自动创建Proxy,它解决了手工创建很多的 ProxyFactoryBean ,导致 xml 配置文件内容复杂,难维护的问题。

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值