zoukankan      html  css  js  c++  java
  • Spring框架之 我对AOP的理解

    Aop 的背景:

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。     

    1.什么是连接点?

    程序执行的某个特定位置:如类开始初始化前、类初始化后、类某个方法调用前、调用后、方法抛出异常后。这些代码中的特定点,称为“连接点”。Spring仅支持方法的连接点,即仅能在方法调用前、方法调用后、方法抛出异常时以及方法调用前后这些程序执行点织入增强。

    2.通知是什么鬼?

    通知(Advice):在切面的某个特定的连接点(Joinpoint)上执行的动作。通知有各种类型,其中包括“around”、“before”和“after”等通知。通知的类型将在后面部分进行讨论。许多AOP框架,包括Spring,都是以拦截器做通知模型, 并维护一个以连接点为中心的拦截器链。

    3、切面(Aspect)
     
    切面由切点和增强组成,它既包括了横切逻辑的定义,也包括了连接点的定义,Spring AOP就是负责实施切面的框架,它将切面所定义的横切逻辑织入到切面所指定的连接点中。

    我从其他博客里搜了下还有人对切面是这么理解的

    切面(Aspect) 切面是通知和切入点的结合。现在发现了吧,没连接点什么事,链接点就是为了让你好理解切点搞出来的,明白这个概念就行了。通知说明了干什么和什么时候干(什么时候通过方法名中的befor,after,around等就能知道),二切入点说明了在哪干(指定到底是哪个方法),这就是一个完整的切面定义。

    这是我的一个Application.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--添加AOP命名空间在AOP这个空间下-->

    <!--此处是指IOC,控制反转,依赖注入-->
    <bean id="happyService" class="cn.happy.service.HappyService">
    <!---这就是依赖注入,为这个对象注入属性值-->
    <property name="info" value="Spring"></property>
    <property name="age" value="16"></property>
    </bean>
    <!--准备一个彩色墨盒-->
    <bean class="cn.happy.pinter.ink.Colorink" id="colorInk"/>
    <!--准备一个B5纸-->
    <bean class="cn.happy.pinter.paper.B5Paper" id="b5Paper"/>


      重难点::

    ///横切关注点被模块话的类叫切面
    //住业务当中的方法叫连接点
    切点可以切到主业务种一个或多个方法的连接点:

    package cn.happy.aop;


    import org.springframework.aop.MethodBeforeAdvice;

    import java.lang.reflect.Method;

    /**
    * Created by Administrator on 2017/7/23.
    */
    public class loggerbefore implements MethodBeforeAdvice {
    ///实现一个MethodBeforeAdvice接口前置通知的方法
    //1此处是切面么?
    //连接点和通知有什么前后关系?
    public void before(Method method, Object[] objects, Object o) throws Throwable {
    System.out.println("========前置增强========");
    }
    }

    <bean class="cn.happy.pinter.paper.A4Paper" id="a4Paper"/>
    <!--准备一台打印机-->
    <bean class="cn.happy.pinter.print.Printer" id="pinter">
    <!--DI 自动依赖注入两个属性-->
    <!--域属性注入使用ref-->
    <property name="ink" ref="colorInk"/>
    <property name="paper" ref="a4Paper"/>
    </bean>

    <!--Userdao 层-->
    <bean id="userdao" class="cn.happy.aop.userDao"></bean>
    <!--Service层-->
    <bean id="userService" class="cn.happy.aop.service.UserBiz">
    <property name="dao" ref="userdao"></property>
    </bean>
    <!--前置增强 Advice通知配置-->
    <!--前置-->
    <bean id="beforeAdvice" class="cn.happy.aop.loggerbefore"></bean>
    <!--after-->
    <bean id="afterAdvice" class="cn.happy.aop.loggerAfter"></bean>
    <!--AOP配置重点来了??、-->
    <aop:config>
    <!--切点:拦截那些业务类的 业务方法--><!--experession切点表达式根据通知上的-->
    <aop:pointcut id="myprintcut" expression="execution(public void save(..))"></aop:pointcut>
    <!--advisor顾问 :对他做前置后置增强-->
    <!--通知绑定到切点上-->
    <aop:advisor advice-ref="beforeAdvice" pointcut-ref="myprintcut"/>
    <aop:advisor advice-ref="afterAdvice" pointcut-ref="myprintcut"/>
    </aop:config>


    </beans>

  • 相关阅读:
    树链剖分( 洛谷P3384 )
    ZJOI 2015 诸神眷顾的幻想乡
    BZOJ 1002 [FJOI2007]轮状病毒
    洛谷 P1485 火枪打怪
    Luogu2860 [USACO06JAN]冗余路径Redundant Paths
    CF962F Simple Cycles Edges
    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
    Luogu2295 MICE
    CF341D Iahub and Xors
    CF617E XOR and Favorite Number
  • 原文地址:https://www.cnblogs.com/hualishu/p/7234698.html
Copyright © 2011-2022 走看看