切面编程(环绕通知与前后置通知区别)

本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html

解决问题

1、拥有前置通知和后置通知的功能,并能解决前置通知和后置通知在共享信息方面的不足(例如:统计切点方法执行时间);

2、在多线程并发条件下,能保证线程安全(因为在一个方法内定义的局部变量);

3、解决代码重复性,降低代码复杂程度;

内容说明

1、以下会给出前置通知、后置通知与环绕通知实例(观众观看表演),通过对比更能理解彼此之间的区别;

2、两者都通过@Component注解,扫描(Audience,Juggler)bean并注册到spring容器中时,需在XML配置文件中引入component-scan(前后置通知:<context:component-scan base-package="com.spring.example.aspectAspectJNoArgs"/> 环绕通知:<context:component-scan base-package="com.spring.example.aspectAround"/>)

3、切面是观众(Audience),切点是节目表演(Performance.perform())
       前置通知:在节目表演之前,观众就坐(调用Audience的takeSeats方法),并关掉手机(调用Audience的turnOffCellPhones方法);
       后置通知:在节目表演结束,观众鼓掌(调用Audience的applaud方法);
       异常通知:节目表演出现异常,观众要求退票(调用Audience的demandRefund方法);

环绕通知:其他与上面相同,只是在节目表演开始与结束时打印时间,统计节目表演时长;

4、通过执行Juggler的perform方法,从而执行切面Audience中相应的方法,达到通知的效果;

应用实例:观众观看表演所做出的相应行为

先列出相关接口以及类代码

节目表演接口(切点方法)

  1. package com.spring.example.aspectAround;
  2.  
  3. /**
  4. * Created by weixw on 2017/11/16.
  5. */
  6. public interface Performer {
  7.  
  8. void perform();
  9. }

切点类实现接口Juggler

  1. package com.spring.example.aspectAround;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. /**
  6. * Created by weixw on 2017/11/16.
  7. */
  8. @Component
  9. public class Juggler implements Performer {
  10. private int beanBags = 3;
  11. public Juggler(){
  12.  
  13. }
  14. public Juggler(int beanBags){
  15. this.beanBags = beanBags ;
  16. }
  17. @Override
  18. public void perform() {
  19. System.out.println("JUGGLING "+ beanBags + " BEANBAGS");
  20. try {
  21. Thread.sleep(1);
  22. }catch (InterruptedException e){
  23. e.printStackTrace();
  24. }
  25. }
  26.  
  27. }

上述代码都能共用,下面分别列举前后置通知与环绕通知区别代码

前后置通知(通过AspectJ注解实现,注意:<aop:aspectj-autoproxy/>不能少,它实现了切面相关方法绑定在切点上,切点方法执行就能触发相应通知)

XML配置文件:spring/aspect-aspectJnoArgs.xml(放在spring文件夹下)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  7.  
  8. <!--使用前置通知和后置通知唯一方式:在前置通知中记录开始时间,并在后置通知中报告表演耗费的时长,必须保存开始时间。因为Audience是单例,如果像这样保-->
  9. <!--存状态,会存在线程安全问题;-->
  10. <context:component-scan base-package="com.spring.example.aspectAspectJNoArgs"/>
  11. <aop:aspectj-autoproxy/>
  12. </beans>

前后置通知切面实现类

  1. package com.spring.example.aspectAspectJNoArgs;
  2.  
  3. import org.aspectj.lang.annotation.*;
  4. import org.springframework.stereotype.Component;
  5.  
  6. /**
  7. * Created by weixw on 2017/11/16.
  8. * 通过AspectJ注解实现切面编程
  9. * 切点方法 id 默认是所依赖方法(public void performance(){})的小写方法名performance
  10. */
  11.  
  12. @Component
  13. @Aspect
  14. public class Audience {
  15. @Pointcut("execution(* com.spring.example.aspectAspectJNoArgs.Performer.perform(..))") //定义切点
  16. public void performance(){}
  17. @Before("performance()")//表演之前
  18. public void takeSeats(){
  19. System.out.println("The audience is taking their seats.");
  20. }
  21. @Before("performance()")//表演之前
  22. public void turnOffCellPhones(){
  23. System.out.println("The audience is turning off their cellphones.");
  24. }
  25. @AfterReturning("performance()")//表演之后
  26. public void applaud(){
  27. System.out.println("CLAP CLAP CLAP CLAP CLAP ");
  28. }
  29. @AfterThrowing("performance()") //表演失败之后
  30. public void demandRefund(){
  31. System.out.println("Boo! We want our money back!");
  32. }
  33. }

环绕通知

XML配置文件:spring/aspect-around.xml(放在spring文件夹下)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  7.  
  8. <!--前置通知和后置通知是在一个方法中实现,所以不需要保存变量值,自然是线程安全的;-->
  9.  
  10. <context:component-scan base-package="com.spring.example.aspectAround"/>
  11. <!--通过component-scan自动扫描,@Component注解将Magician注册到spring容器-->
  12. <aop:config>
  13. <!--audience :切面 watchPerformance:切面方法 performance:切点-->
  14. <aop:aspect ref="audience">
  15. <aop:pointcut id="performance" expression="execution(* com.spring.example.aspectAround.Performer.perform(..))"/>
  16. <aop:around pointcut-ref="performance" method="watchPerformance" />
  17. </aop:aspect>
  18. </aop:config>
  19. </beans>

环绕通知切面实现类

  1. package com.spring.example.aspectAround;
  2.  
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.springframework.stereotype.Component;
  5.  
  6. /**
  7. * Created by weixw on 2017/11/16.
  8. */
  9. @Component
  10. public class Audience {
  11. public void takeSeats(){
  12. System.out.println("The audience is taking their seats.");
  13. }
  14. public void turnOffCellPhones(){
  15. System.out.println("The audience is turning off their cellphones.");
  16. }
  17. public void applaud(){
  18. System.out.println("CLAP CLAP CLAP CLAP CLAP");
  19. }
  20. public void demandRefund(){
  21. System.out.println("Boo! We want our money back!");
  22. }
  23.  
  24. public void watchPerformance(ProceedingJoinPoint joinPoint){
  25. try{
  26. takeSeats(); //表演之前
  27. turnOffCellPhones(); //表演之前
  28. long start = System.currentTimeMillis();
  29. System.out.println("The performance start ......");//节目开始
  30. joinPoint.proceed(); //执行被通知的方法
  31. System.out.println("The performance end ......");//节目结束
  32. long end = System.currentTimeMillis(); //表演之后
  33. applaud();//表演之后
  34. System.out.println("The performance took milliseconds:"+ (end - start) );//表演时长
  35. }catch (Throwable t){
  36. demandRefund(); //表演失败之后
  37. }
  38. }
  39. }

测试代码

环绕通知测试代码如下,前后置通知测试代码只需将配置文件名称改成spring/aspect-aspectJnoArgs.xml即可

  1. package com.spring.example.aspectAround;/**
  2. * Created by weixw on 2017/11/16.
  3. */
  4.  
  5. import javafx.application.Application;
  6. import javafx.stage.Stage;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9.  
  10. public class Driver extends Application {
  11.  
  12. public static void main(String[] args) {
  13. launch(args);
  14. }
  15.  
  16. @Override
  17. public void start(Stage primaryStage) {
  18. try {
  19.  
  20. ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/aspect-around.xml");
  21. Performer performer = (Performer) ctx.getBean("juggler");
  22. performer.perform();
  23.  
  24. }catch (Exception e){
  25. e.printStackTrace();
  26. }
  27. }
  28. }

运行结果

环绕通知结果:

前后置通知结果:

总结

上述列出前后置通知和环绕通知样例。对于有变量缓存需求,线程安全的应用场景,前后置通知实现比较困难,而环绕通知实现就非常容易;

不要让懒惰占据你的大脑,不要让妥协拖垮你的人生。青春就是一张票,能不能赶上时代的快车,你的步伐掌握在你的脚下。

spring框架应用系列四:切面编程(环绕通知与前后置通知区别)的更多相关文章

  1. Spring实战4:面向切面编程

    主要内容 面向切面编程的基本知识 为POJO创建切面 使用@AspectJ注解 为AspectJ的aspects注入依赖关系 在南方没有暖气的冬天,太冷了,非常想念北方有暖气的冬天.为了取暖,很多朋友 ...

  2. Spring框架的第四天(整合ssh框架)

    ## Spring框架的第四天 ## ---------- **课程回顾:Spring框架第三天** 1. AOP注解方式 * 编写切面类(包含通知和切入点) * 开启自动代理 2. JDBC模板技术 ...

  3. Spring Boot之AOP面向切面编程-实战篇

    目录 前言 编程范式主要有以下几类 引入pom依赖 aop注解 实现日志分割功能 前言 AOP是一种与语言无关的程序思想.编程范式.项目业务逻辑中,将通用的模块以水平切割的方式进行分离统一处理,常用于 ...

  4. Spring AOP前置通知和后置通知

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...

  5. spring框架应用系列三:切面编程(带参数)

    本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7786715.html 解决问题 1.分离业务监控与业务处理.简单点 ...

  6. Spring Boot2(六):使用Spring Boot整合AOP面向切面编程

    一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop ​ aop全称Aspec ...

  7. Spring(三)面向切面编程(AOP)

    在直系学长曾经的指导下,参考了直系学长的博客(https://www.cnblogs.com/WellHold/p/6655769.html)学习Spring的另一个核心概念--面向切片编程,即AOP ...

  8. Spring中AOP简介与切面编程的使用

    Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...

  9. 02 浅析Spring的AOP(面向切面编程)

    1.关于AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...

随机推荐

  1. 编译ROCKSDB总结

    Rocksdb是挺好的一个东西,就是取得一个可用的库太麻烦.之前我是用的rocksdbsharp里面他有编译好windows 和 linux的库 兼 容性还挺好,ubuntu win10 直接跑没毛病 ...

  2. 03SQLALchemy外键约束

    一,配置 1,SQLALchemy的配置单独使用config.py文件2,满足如下要求: #dialect+driver://username:password@host:port/database ...

  3. ARTS Challenge- Week 1 (2019.03.25~2019.03.31)

    1.Algorithm - at least one leetcode problem per week(Medium+) 986. Interval List Intersections https ...

  4. go 统计目录大小

    文件大小获取 // 这里获取的是 FileInfo 对象 fi, _ := os.Stat(filepath) FileInfo 定义如下: type FileInfo interface { Nam ...

  5. 微信JS SDK接入的几点注意事项

    微信JS SDK接入,主要可以先参考官网说明文档,总结起来有几个步骤: 1.绑定域名:先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”.备注:登录后可在“开发者中心”查看对 ...

  6. tmux使用中出现的问题和解决方式

    常用操作: tmux ls 看当前都有哪些sessiontmux new -s my1 创建窗口,名为my1ctrl+B,D 退出窗口 (这个就是同时按ctrl和B,然后松开后再按D键)tmux at ...

  7. 使用Axis2创建一个简单的WebService服务

    使用过Java进行过WebService开发都会听过或者接触过Apache Axis2,Axis2框架是应用最广泛的WebService框架之一了. 这里使用Axis2来开发和部署一个最简单的WebS ...

  8. 关于如何在Visual Studio上仿真调试安卓的U3D应用

    正巧最近需要开发一个安卓手机上的Unity3D游戏功能,想着既然要开发么,当然需要调试.本来的话一些基础功能是不需要使用仿真模拟器,直接在U3D的开发编辑器上就能调试,不过有一些安卓上才能执行,比如 ...

  9. python—迭代器、生成器

    1.迭代器(iteration) 迭代器协议:提供next()方法,该方法要么返回迭代的下一项,要么异常.(只能往后走) 可迭代对象:实现迭代器协议的对象. **字符串.列表.元祖.字典.集合.文件都 ...

  10. json转义 使用 JavaScriptSerializer 时 需要添加的引用

    当创建JavaScriptSerializer创建对象时,JavaScriptSerializer jss=new JavaScriptSerializer():时. 1.   需要添加的是Syste ...