能源项目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();
    }

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

原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6031281.html