zoukankan      html  css  js  c++  java
  • 对Spring 框架 AOP(面向切面)的理解

    AOP : 面向切面变成 采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存)

    利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率

    AOP的几个核心概念”:

    1,横切关注点

       对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

    2,切面

       类是对物体特征的抽象,切面就是对横切关注点的抽象

    3、连接点(joinpoint

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

    service 实现类中 的所有方法 都有可能成为连接点

    4、切入点(pointcut

    对连接点进行拦截的定义

    所谓切入点是指我们要对哪些Joinpoint进行拦截的定义

    5、通知(advice

    所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)

    6、目标对象

    代理的目标对象

    7、织入(weave)

    将切面应用到目标对象并导致代理对象创建的过程

    8、引入(introduction)

    在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

    spring 框架 AOP的配置

    1,创建JavaWEB项目,引入具体的开发的jar包
            * 先引入Spring框架开发的基本开发包
            * 再引入Spring框架的AOP的开发包
                * spring的传统AOP的开发的包
                    * spring-aop-4.2.4.RELEASE.jar
                    * com.springsource.org.aopalliance-1.0.0.jar
                
                * aspectJ的开发包
                    * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
                    * spring-aspects-4.2.4.RELEASE.jar

    具体的包版本 根据实际需求进行引入

    2,创建Spring的配置文件,引入具体的AOP的schema约束 ‘’

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
         ">

    3,创建包结构,编写具体的接口和实现类

    就是项目中service接口 和 实现类

    4,将目标类配置到Spring中(将相应的接口实现类注入到Spring中)   该步骤 在ssm框架中 可以省略

      <bean id="customerDao" class="com.itheima.demo3.CustomerDaoImpl"/>

    目标类:接口的实现类

    5,定义切面类

    6,在配置文件中定义切面类

    <bean id="logAspect" class="com.meyki.common.log.LogAspect" />

    7,在配置文件中完成aop的配置 

    <!-- 定义接口方式,代理指定类 -->
        <aop:config>
                   //引入切面类
            <aop:aspect ref="logAspect">
                     //定义通知类型,切入点的确定其中@annotation 在applicationContext.xml 配置文件夹
                      <aop:around method="doAround" pointcut="@annotation(com.meyki.common.log.ActionLog)" />
    </aop:aspect>
    </aop:config>

    再配置切入点的时候,需要定义表达式,重点的格式如下:execution(public * *(..)),具体展开如下:
            * 切入点表达式的格式如下:
                * execution([修饰符] 返回值类型 包名.类名.方法名(参数))

     <aop:config>
            <aop:pointcut id="pointcutMethod" expression="execution (* com.*.*.*.service.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutMethod" />
        </aop:config>

      在配置文件中开启自动代理

    <aop:aspectj-autoproxy/>

     

  • 相关阅读:
    jzoj 3176. 【GDOI2013模拟5】蜘蛛侠
    各种各样的根号算法 总结&刷题
    jzoj 3187. 【GDOI2013模拟8】的士
    jzoj 3188. 【GDOI2013模拟8】找数
    jzoj 4673. 【NOIP2016提高A组模拟7.20】LCS again
    jzoj 4672. 【NOIP2016提高A组模拟7.20】Graph Coloring
    markdown 模板2
    树莓派kali开启arp【arpspoof,urlsnarf】
    Java 图片处理——如何生成高清晰度而占有磁盘小的缩略图
    手把手教你生成二维码-google.zxing
  • 原文地址:https://www.cnblogs.com/wumingxuanji/p/8574694.html
Copyright © 2011-2022 走看看