zoukankan      html  css  js  c++  java
  • AOP学习(2)


    <
    property name="interceptorNames"> <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们 也可以把通知看出拦截器,structs2核心拦截器 --> <!-- 多个value是数组注入,需要使用list --> <list> <value>MyMethodBeforeAdvice</value> <!-- 织入后置通知 --> <value>myAfterReturningAdvice</value></list> </property>

    name是不能瞎写的,因为在ProxyFactoryBean中调用的方法名是:

    setInterceptorNames()

    提一个问题

    class A{
    
    //private String name;
    
    public void setName(String name){
    
    System.out.println("name"+name);
    
    }
    
    }
    
    beans.xml
    
    <bean id="a" class="...A">
    
    <property name="name" value="顺平"/>
    
    </bean>

    A a=new A();

    a.setName("顺平");

    不是看A中的属性,而是看beans.xml中的属性。

    比如方法名为setEE(),就去设置属性eE.

    ①前置通知

    ②后置通知

    ③环绕通知

     拦截对目标方法的调用

    环绕通知,好像把真正要执行的动作包围了。

    ④异常通知

    当目标方法抛出异常后自动调用

    ⑤引入通知

    自定义切入点

     

    所用beans.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" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
    
        xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          ">
        <!-- 配置被代理的对象 -->
        <bean id="test1Service" class="com.hsp.aop.Test1Service">
            <property name="name" value="顺平" />
        </bean>
    
    
        <!-- 配置前置通知 -->
        <bean id="MyMethodBeforeAdvice" class="com.hsp.aop.MyMethodBeforeAdvice">
        </bean>
    
        <!-- 配置后置通知 -->
        <bean id="myAfterReturningAdvice" class="com.hsp.aop.MyAfterReturningAdvice" />
    
        <!-- 配置环绕通知 -->
        <bean id="myMethodInterceptor" class="com.hsp.aop.MyMethodInterceptor" />
    
        <!-- 配置异常通知 -->
        <bean id="myThrowsAdvice" class="com.hsp.aop.MyThrowsAdvice" />
        <!-- 定义前置通知的切入点 -->
        <bean id="myMethodBeforeAdviceFilter"
            class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
            <property name="advice" ref="MyMethodBeforeAdvice" />
            <property name="mappedNames">
                <list>
                    <value>sayHello</value>
                </list>
            </property>
        </bean>
        <!-- 配置代理对象 -->
        <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    
            <!-- 代理的接口集 -->
            <property name="proxyInterfaces">
                <list>
                    <value>com.hsp.aop.TestServiceInter</value>
                    <value>com.hsp.aop.TestServiceInter2</value>
                </list>
            </property>
            <!-- 把通知织入到代理对象 -->
            <property name="interceptorNames">
                <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们 也可以把通知看出拦截器,structs2核心拦截器 -->
                <!-- 多个value是数组注入,需要使用list -->
                <list>
                    <!-- 使用自定义切入点来控制前置通知 -->
                    <value>myMethodBeforeAdviceFilter</value>
                    <!-- 织入后置通知 -->
                    <value>myAfterReturningAdvice</value>
                    <!-- 织入环绕通知 -->
                    <value>myMethodInterceptor</value>
                    <!-- 织入异常通知 -->
                    <value>myThrowsAdvice</value>
                </list>
            </property>
            <!-- 配置被代理对象,可以指定 -->
            <property name="target" ref="test1Service">
            </property>
        </bean>
    </beans>

    获取的动态代理对象是不是就是bean里面引入的类型,而是动态代理对象类型。

    如果它实现了接口,走的就是java jdk里的封口类型,如果没有实现接口,走的就是gc lib这种动态代理技术。

    提问?spring的AOP中,当你通过代理对象去实现aop的时候,获取的ProxyFactoryBean是什么类型?

    答:返回的是一个代理对象。如果目标对象实现了接口,则spring使用jdk的动态代理技术。如果目标对象没有实现接口,则spring使用CGLIB技术。

    切入点运行使用正则表达式来

  • 相关阅读:
    (44)FreeRTOS学习之一
    (43)软件架构设计思想总结
    (42)嵌入式项目中常用到的C语言技能总结
    (41)freeRTOS之任务管理
    (40)每个新手程序员都会犯的5个错误
    (39)23种设计模式研究之十【状态模式】
    (38)23种设计模式研究之九【迭代器模式和组合模式】
    (37)23种设计模式研究之八【模板方法模式】
    (36)23种设计模式研究之七【适配器模式和外观模式】
    (35)23种设计模式研究之六【命令模式】
  • 原文地址:https://www.cnblogs.com/liaoxiaolao/p/9888653.html
Copyright © 2011-2022 走看看