美文网首页
Spring Aop:二、自动创建代理(Proxy)

Spring Aop:二、自动创建代理(Proxy)

作者: weihy | 来源:发表于2019-11-01 12:07 被阅读0次

本文参考实验楼教程:https://www.shiyanlou.com/courses/578/learning/?id=1940

 在上一篇文章中Spring-Aop:一、四种Advice(本文沿用上一篇文章的实验内容),我们必须为每一个需要AOP的bean创建一个Proxy(比如:customerService 与 customerServiceProxy)。
 这不是一个很好的体验,例如我们想为DAO层的所有bean都支持AOP,以便写SQL日志,那么必须手工在xml配置文件中一个一个的为他们创建ProxyFactoryBean,这样会直接导致xml文件身份复杂并且臃肿,不利于维护。

 Spring中,提供了两种自动创建代理的方法:

  • 利用BeanNameAutoProxyCreator自动创建Proxy
  • 利用DefaultAdvisorAutoProxyCreator自动创建Proxy
1、手动创建ProxyFactoryBean实例

 配置好Advisor之后,手动创建ProxyFactoryBean并将其interceptorNames配置上Advisor。

<?xml version="1.0" encoding="UTF-8"?>
<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="weihouye"/>
        <property name="url" value="www.weihouyeaiyangzhan.com"/>
    </bean>

    <!--<bean id="weiBeforeMethod" class="com.shiyanlou.spring.aop.advice.WeiBeforeMethod"/>-->
    <!--<bean id="weiAfterMethod" class="com.shiyanlou.spring.aop.advice.WeiAfterMethod"/>-->
    <!--<bean id="weiThrowExceptionMethod" class="com.shiyanlou.spring.aop.advice.WeiThrowExceptionMethod"/>-->
    <bean id="weiAroundMethod" class="com.shiyanlou.spring.aop.advice.WeiAroundMethod"/>

    <!--<bean id="customerPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">-->
        <!--<property name="mappedName" value="printName"/>-->
    <!--</bean>-->

    <!--<bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">-->
        <!--<property name="pointcut" ref="customerPointcut"/>-->
        <!--<property name="advice" ref="weiAroundMethod"/>-->
    <!--</bean>-->

    <!--pointcut和advisor可以一起配置,只要在配置customerAdvisor时,选择NameMatchMethodPointcutAdvisor-->
    <!--<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">-->
        <!--<property name="mappedName" value="printName"/>-->
        <!--<property name="advice" ref="weiAroundMethod"/>-->
    <!--</bean>-->

    <bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="patterns">
            <list>
                <value>.*URL.*</value>
            </list>
        </property>
        <property name="advice" ref="weiAroundMethod"/>
    </bean>

    <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="customerService"/>
        <property name="interceptorNames">
            <list>
                <!--<value>weiBeforeMethod</value>-->
                <!--<value>weiAfterMethod</value>-->
                <!--<value>weiThrowExceptionMethod</value>-->
                <value>customerAdvisor</value>
            </list>
        </property>
    </bean>
</beans>

配置完xml文件,还要在App.java中得到customerServiceProxy,代码如下:


        context = new ClassPathXmlApplicationContext("SpringAopAdvice.xml");
        CustomerService cust = (CustomerService) context.getBean("customerServiceProxy");

2、利用BeanNameAutoProxyCreator自动创建Proxy

 在这种方法中,我们需要创建BeanNameAutoProxyCreator,将所有的Bean通过正则或者名字匹配与Advisor关联,SpringAopAdvice.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<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="weihouye"/>
        <property name="url" value="www.weihouyeaiyangzhan.com"/>
    </bean>

    <bean id="weiAroundMethod" class="com.shiyanlou.spring.aop.advice.WeiAroundMethod"/>

    <bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="patterns">
            <list>
                <value>.*URL.*</value>
            </list>
        </property>
        <property name="advice" ref="weiAroundMethod"/>
    </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>

 以上配置中只要bean 的id符合*Service就会自动创建Proxy,所以,可以使用一下代码获取Proxy:

        context = new ClassPathXmlApplicationContext("SpringAopAdvice.xml");

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

 运行结果如下:

*************************
Customer name: weihouye
*************************
Method Name: printURL
Method Arguments: []
WeiBeforeMethod: Before method hi wei!!!
Customer URL: www.weihouyeaiyangzhan.com
WeiAfterMethod: After Method hi wei!!
*************************
3、利用DefaultAdvisorAutoProxyCreator自动创建Proxy

  利用DefaultAdvisorProxyCreator自动创建代理,任何匹配Advisor的Bean,都会自动创建Proxy实现AOP,所以慎用。

<?xml version="1.0" encoding="UTF-8"?>
<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="weihouye"/>
        <property name="url" value="www.weihouyeaiyangzhan.com"/>
    </bean>

    <bean id="weiAroundMethod" class="com.shiyanlou.spring.aop.advice.WeiAroundMethod"/>

    <bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="patterns">
            <list>
                <value>.*URL.*</value>
            </list>
        </property>
        <property name="advice" ref="weiAroundMethod"/>
    </bean>

    <!--利用DefaultAdvisorAutoProxyCreator自动创建代理-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

</beans>

 以上配置,容器中的bean,有方法名和.*URL.*匹配的方法,在使用时,都会自动创建Proxy,如下获取代理:

        context = new ClassPathXmlApplicationContext("SpringAopAdvice.xml");

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

实验结果同上。

4、个人总结

 从实验结果看来,Spring Aop编程有三个要点:

  1. 四种通知Advices 定义了,拦截器的代码在被拦截类的方法中执行的时机:
  • Before Advice —— 被代理类方法执行前,执行拦截器代码;
  • After Returning Advice —— 被代理类方法执行后(且无异常);
  • After Throwing Advice —— 被代理类方法执行抛出异常后;
  • Around advice —— 环绕通知,结合了以上三种
  1. Pointcut 定义了要拦截被代理类的哪个方法,通过正则匹配或者方法名匹配。Advisor 将 Advice 和 Pointcut 结合组成独立的单元,可以传给ProxyFactoryBean
  2. 第三点要定义容器中的哪些bean需要作为被代理类,为它创建相应的代理类;
  • 手动创建代理 —— 创建ProxyFactoryBean,指定target属性(被代理的bean),指定interceptorNames(拦截器)
  • 自动创建代理 —— 利用BeanNameAutoProxyCreatorDefaultAdvisorAutoProxyCreator自动创建Proxy

相关文章

  • Spring Aop:二、自动创建代理(Proxy)

    本文参考实验楼教程:https://www.shiyanlou.com/courses/578/learning/...

  • spring-AOP(二) 自动代理

    spring-AOP(二) 自动代理 知识导读 在何时何处创建代理对象,如何能覆盖被代理对象,返回代理后的对象 自...

  • Spring AOP从原理到源码(三)

    接着上一节Spring AOP从原理到源码(二),本节关注spring aop创建代理对象的过程。 Spring ...

  • Spring AOP(一)

    Spring AOP实现原理 动态代理: 利用核心类Proxy和接口InvocationHandler(基于代理模...

  • Spring AOP 与 AspectJ

    spring AOP 基于代理(Proxy)的方式实现AOP实现的方式是运行时代理具体细节可以参考JDK动态代理[...

  • Spring AOP 自动创建代理

    1. 代理创建器 自动创建代理对象,将一个切面织入到多个目标对象中 三种自动代理创建器BeanNameAutoPr...

  • 2020-02-26

    Spring的AOP代理模式 三种实现 1. 通过Proxy的newProxyInstance(类加载器,代理对象...

  • AOP代理:

    AOP代理:AOP框架创建的对象,代理就是对目标对象的增强。Spring中的AOP代理可以是JDK动态代理,也可以...

  • Java动态代理简析原理

    说下Java动态代理,Spring的AOP就是基于Java的动态代理实现的。动态代理用到的几个类和接口,Proxy...

  • 27、Spring5之AOP底层原理(JDK动态代理实现)

    AOP(JDK动态代理) 1、使用JDK动态代理,使用Proxy类里面的方法创建代理对象 (1)调用newProx...

网友评论

      本文标题:Spring Aop:二、自动创建代理(Proxy)

      本文链接:https://www.haomeiwen.com/subject/jontbctx.html