观众类Audience~~

[java] view plain copy
  1. package com.jCuckoo.demo;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. public class Audience {
  4. public void takeSeats() {
  5. System.out.println("----开演之前,请占座----");
  6. }
  7. public void turnOffCellPhones() {
  8. System.out.println("----开始之前,请关机----");
  9. }
  10. public void applaud() {
  11. System.out.println("****鼓掌,继续鼓掌。****");
  12. }
  13. public void turnOnCellPhones() {
  14. System.out.println("****演出结束,可以开机****");
  15. }
  16. public void demandRefund() {
  17. System.out.println("
    太熊了,退我票钱

    ");

  18. }
  19. public void watchPerformance(ProceedingJoinPoint joinpoint) {
  20. try {
  21. System.out.println("oooooooo环绕通知开始oooooooo");
  22. long start = System.currentTimeMillis();
  23. joinpoint.proceed();
  24. long end = System.currentTimeMillis();
  25. System.out.println("oooooooo环绕通知结束oooooooo");
  26. System.out.println("演出耗时共计:" + (end - start)
  27. + "毫秒。");
  28. } catch (Throwable t) {
  29. System.out.println("Boo!Wewantourmoneyback!");
  30. }
  31. }
  32. }

表演接口Performer

[java] view plain copy
  1. package com.jCuckoo.demo;
  2. public interface Performer {
  3. void perform()throws Exception;
  4. }

定义魔术师Juggler,实现表演接口

[java] view plain copy
  1. package com.jCuckoo.demo;
  2. public class Juggler implements Performer {
  3. private int beanBags = 3;
  4. public Juggler() {
  5. }
  6. public Juggler(int beanBags) {
  7. this.beanBags = beanBags;
  8. }
  9. public void perform() throws Exception {
  10. System.out.println("表演开始:魔术师欺骗了" + beanBags + "个游戏豆。");
  11. }
  12. }

spring的配置文档applicationContext.xml

[html] view plain copy
  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" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  12. <bean id="juggler" class="com.jCuckoo.demo.Juggler"/>
  13. <bean id="audience" class="com.jCuckoo.demo.Audience" />
  14. <aop:config>
  15. <aop:aspect ref="audience">
  16. <aop:pointcut id="performance"
  17. expression="execution(* com.jCuckoo.demo.Performer.perform(..))" />
  18. <aop:before pointcut-ref="performance" method="takeSeats" />
  19. <aop:before pointcut-ref="performance" method="turnOffCellPhones" />
  20. <aop:after pointcut-ref="performance" method="turnOnCellPhones" />
  21. <aop:after-returning pointcut-ref="performance"
  22. method="applaud" />
  23. <aop:after-throwing pointcut-ref="performance"
  24. method="demandRefund" />
  25. <aop:around pointcut-ref="performance" method="watchPerformance"/>
  26. </aop:aspect>
  27. </aop:config>
  28. </beans>

测试类,获取魔术师,并进行表演。

[java] view plain copy
  1. package com.jCuckoo.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.jCuckoo.demo.Performer;
  5. public class MainTest {
  6. /**
  7. * @param args
  8. */
  9. public static void main(String[] args) {
  10. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
  11. Performer performer=(Performer)ctx.getBean("juggler");
  12. try {
  13. performer.perform();
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }

最终结果:

----开演之前,请占座----
----开始之前,请关机----
oooooooo环绕通知开始oooooooo
表演开始:魔术师欺骗了3个游戏豆。
****演出结束,可以开机****
****鼓掌,继续鼓掌。****
oooooooo环绕通知结束oooooooo
演出耗时共计:1毫秒。

Spring3.0中的前置通知、后置通知、环绕通知、异常通知相关推荐

  1. spring的几个通知(前置、后置、环绕、异常、最终)

    1.没有异常的 2.有异常的 1.被代理类接口Person.java 1 package com.xiaostudy; 2 3 /** 4 * @desc 被代理类接口 5 * 6 * @author ...

  2. 前置,后置,环绕,异常增强(示例)

    ----------------------------------基础架构: 前置增强 ------------------------------------代码演示: public interf ...

  3. c++中的前置和后置加加没你想的这么简单

    1.前置++和后置++的区别1: 前置是先加后赋值,后置是先赋值再加. 2.前置++比后置++运行的速度快: 在汇编底层中后置++会比前置++多一行. 当然我这demo不是很准确毕竟也是有偶然的,但是 ...

  4. Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)

    AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...

  5. python测试框架之Pytest(二) : 测试用例的前置setup后置teardown用法

    目录 pytest中的前置和后置 类内外执行:方法细化级setup/teardown 类外执行:模块级(setup_module/teardown_module) 类外执行:函数级(setup_fun ...

  6. cad怎么将图层后置_CAD中如何将图形前置和后置吗? - CAD自学网

    如果图中都是线性图形且最终要单色打印,通常不用太理会图形的顺序.但如果实体填充且与其他图形有重叠,就需要设置图形的顺序了,有时我们在插入一些设备或构件时,为了图面更整洁,会在图块中添加区域覆盖WIPE ...

  7. cad怎么将图层后置_CAD中如何将图形前置和后置吗?

    如果图中都是线性图形且最终要单色打印,通常不用太理会图形的顺序.但如果实体填充且与其他图形有重叠,就需要设置图形的顺序了,有时我们在插入一些设备或构件时,为了图面更整洁,会在图块中添加区域覆盖WIPE ...

  8. PHP通过__call实现简单的AOP(主事务后的其他操作)比如前置通知,后置通知

    /*** person class*/ class Person {/*** person class -> function say*/public static function say($ ...

  9. # c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符...

    c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符 标签(空格分隔): c++ 前言 我在c++学习的过程中, 对这几个不太常见的运算符重载不太会写.出现了很 ...

最新文章

  1. 多媒体计算机系统是能进行获取,第六章 7 多媒体计算机系统.pdf
  2. [Django] 查看orm自己主动运行的原始查询sql
  3. cassandra可视化工具_程序员绘图工具——PlantUML
  4. String,StringBuffer,StringBuilder
  5. 编程关键词介绍...
  6. python中if语句使用_如何在python中使用'空if语句'?
  7. GNN上用到的Tasks,Dataset and Benchmark
  8. spring-boot-starter-parent的主要作用
  9. 黑大选修计算机模拟物理学,黑龙江大学学分制选课指南.doc
  10. 【前端 · 面试 】HTTP 总结(四)—— HTTP 状态码
  11. Android蓝牙服务
  12. java模拟时钟课程设计_Java课程设计时钟图形模拟
  13. QT自定义3D图形控件,支持轴拖动。
  14. Web前端开发技术实验与实践(第3版)储久良编著实训11
  15. winform程序:newtonsoft json 序列化时出现 “unterminated string. Excepted delimiter...
  16. 433MHz,2.4GHz,GPRS,NB-IOT各有哪些特点?
  17. 同源性 相似性 一致性
  18. (转)2007北京高考作文题目(太有才了.我都看了无数遍了)
  19. C++课程设计快递业务管理系统实验报告
  20. 构成社群的五要素,你了解吗?

热门文章

  1. hdu2438 三分
  2. 【错误记录】Android Gradle 配置报错 ( gradle.properties 配置到 BuildConfig 中需要注意类型转换 | 位置: 类 BuildConfig )
  3. 大数据笔记(六)——HDFS的底层原理:JAVA动态代理和RPC
  4. bzoj 3223: Tyvj 1729 文艺平衡树
  5. 【eoe教程】Android中自定义视图的绘制方法
  6. servlet中的几个路径有关的方法
  7. 从JVM看类的加载过程与对象实例化过程
  8. 深入理解C/C++二维数组
  9. 如何玩转跨库Join?跨数据库实例查询应用实践
  10. 工信部部长苗圩于CITE发表致辞,指引三个方向推动国内电子信息产业持续发展...