AOP

来源:互联网 发布:软件项目管理常见问题 编辑:程序博客网 时间:2024/05/29 14:30

知识点

  • 自动创建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 配置文件内容复杂,难维护的问题。

原创粉丝点击