zoukankan      html  css  js  c++  java
  • 能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator

    [Spring]AOP拦截-三种方式实现自动代理

    这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframework.aop.framework.ProxyFactoryBean的bean。

    用Spring一个自动代理类DefaultAdvisorAutoProxyCreator:

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

    例如: 
    原来不用自动代理的配置文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
        <!-- 代理前原对象 -->
        <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>
    
        <!-- 切面 = 切点+通知 -->
        <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <!-- 切点 -->
            <property name="patterns">
                <list>
                    <value>.*run.*</value>
                </list>
            </property>
            <!-- 通知-由我们写,实际代理动作 -->
            <property name="advice">
                <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean>
            </property>
        </bean>
    
        <!-- 代理工厂 -->
        <bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!-- 放入原型对象 -->
            <property name="target" ref="person"></property>
    
            <!-- 放入切面 -->
            <property name="interceptorNames">
                <list>
                    <value>advisor</value>
                </list>
            </property>
        </bean>
    </beans>

    现在改用自动代理,如下配置:

    <beans ...>
    <!-- 代理前原对象 -->
        <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>
    
        <!-- 切面 = 切点+通知 -->
        <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <!-- 切点 -->
            <property name="patterns">
                <list>
                    <value>.*run.*</value>
                </list>
            </property>
            <!-- 通知-由我们写,实际代理动作 -->
            <property name="advice">
                <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean>
            </property>
        </bean>

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

    测试方法

      @Test//自动代理
        public void demo4(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/4.xml");
            //我们直接在这里获取Person对象就可以了,因为在最开始xml文件newPerson对象后,Spring就已经帮我们代理了!
            Person p =ctx.getBean(Person.class);
            p.run();
            p.say();
        }

    相对于前面,也就是把代理工厂部分换成自动代理了。

  • 相关阅读:
    《移动开发者周刊》第十一期
    2012安卓巴士开发者沙龙成都站大家抓紧报名
    23岁那年你正处在哪个状态?现在呢?
    《老罗Android开发视频教程》老罗来交国庆的答卷了
    程序员,你的一千万在哪里?
    《老罗Android开发视频教程》更新
    2012全球开发者大会项目投资一对一相亲会
    windows远程桌面
    [LeetCode] NQueens
    [LeetCode] Pascal's Triangle II
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6031281.html
Copyright © 2011-2022 走看看