首页 > 编程语言 > 详细

Spring 中AOP及前后置增强案例

时间:2019-10-26 16:20:23      阅读:90      评论:0      收藏:0      [点我收藏+]

1、AOP面向切面编程

  面向切面编程的本质:面向切面编程,指扩展功能不修改源代码,将功能代码从业务逻辑代码中分离出来。

         1主要功能:日志记录,性能统计,安全控制,事务处理,异常处理等等。

         2主要意图:将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。

2、AOP原理

  • 将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决
  • 采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。

3、AOP相关术语

             1 增强处理类型

  • 前置增强:目标方法调用前
  • 后置增强:目标方法调用后
  • 环绕增强:前置+后置
  • 异常抛出增强:只有在目标方法抛出异常时才执行
  • 最终增强:finally

 

              2 AOP的设计单元

术语名称

描述

切面(Aspect)

横切关注点而被模块化的类,称之为切面

通知(Advice)

切面要完成的工作,具体指代切面类中的某个方法

连接点(Join Point)

一个应用在执行的过程中能被插入切面的一个点

切点(Point Cut)

通配表达式,可以匹配一个或多个连接点

织入(Weaving)

将切面应用到目标对象的代理对象的过程

4、AOP案例实现增强

  (1)导入依赖

    技术分享图片

 

  (2)实体类UserInfo 实现Serializable接口

    技术分享图片

 

   (3)mapper中IUserInfoMapper接口

    技术分享图片

 

  (4)mapper接口的实现类IUserInfoMapperImpl

    技术分享图片

 

 

  (5)service中IUserInfoService接口

    技术分享图片

 

  (6)service中接口实现类IUserInfoServiceImpl

    技术分享图片

 

 

 

   (7)编码方式实现AOP

    技术分享图片

  

  (8)增强类

    技术分享图片

 

  (9)配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">

    <!--声明Daobean      bean的注入注入的都是实现类-->
    <bean id="iUserInfoMapper" class="cn.spring.mapper.impl.IUserInfoMapperImpl"></bean>
    <!--声明Service-->
    <bean id="iUserInfoService" class="cn.spring.service.impl.IUserInfoServiceImpl">
        <!--setter方法怎么注入:找到Name属性值,将属性值的开头改为大写,然后前缀加上setIUserInfoMapper-->
        <property name="iUserInfoMapper" ref="iUserInfoMapper"></property>
    </bean>


    <!--切面:增强类-->
    <bean id="myAdvice" class="cn.spring.advice.MyAdvice"></bean>
    <!--增加处理
        切点:你要对哪一个方法进行增强        expression:切点表达式匹配的
                  execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)
                        modifiers-pattern:方法的可见性,如public,protected;
                        ret-type-pattern:方法的返回值类型,如int,void等;
                        declaring-type-pattern:方法所在类的全路径名,如com.spring.Aspect;
                        name-pattern:方法名类型,如buisinessService();
                        param-pattern:方法的参数类型,如java.lang.String;
                        throws-pattern:方法抛出的异常类型,如java.lang.Exception;
                  通配符:
                    *通配符,该通配符主要用于匹配单个单词,或者是以某个词为前缀或后缀的单词。
                    ..通配符,该通配符表示0个或多个项
    -->
    <aop:config>
        <!--切点-->
        <aop:pointcut id="pointcut" expression="execution(* *..service.*.*(..))"/>
        <!--织入   将增强处理和切点表达式符合的方法关联到一起-->
        <aop:advisor advice-ref="myAdvice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

 

   (10)测试类

    技术分享图片

 

 

   (11)控制台 实现增强

    技术分享图片

 

Spring 中AOP及前后置增强案例

原文:https://www.cnblogs.com/Zzzzn/p/11743302.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!