Spring3.0中的前置通知、后置通知、环绕通知、异常通知
观众类Audience~~
- package com.jCuckoo.demo;
- import org.aspectj.lang.ProceedingJoinPoint;
- public class Audience {
- public void takeSeats() {
- System.out.println("----开演之前,请占座----");
- }
- public void turnOffCellPhones() {
- System.out.println("----开始之前,请关机----");
- }
- public void applaud() {
- System.out.println("****鼓掌,继续鼓掌。****");
- }
- public void turnOnCellPhones() {
- System.out.println("****演出结束,可以开机****");
- }
- public void demandRefund() {
- System.out.println("
太熊了,退我票钱
");
- }
- public void watchPerformance(ProceedingJoinPoint joinpoint) {
- try {
- System.out.println("oooooooo环绕通知开始oooooooo");
- long start = System.currentTimeMillis();
- joinpoint.proceed();
- long end = System.currentTimeMillis();
- System.out.println("oooooooo环绕通知结束oooooooo");
- System.out.println("演出耗时共计:" + (end - start)
- + "毫秒。");
- } catch (Throwable t) {
- System.out.println("Boo!Wewantourmoneyback!");
- }
- }
- }
表演接口Performer
- package com.jCuckoo.demo;
- public interface Performer {
- void perform()throws Exception;
- }
定义魔术师Juggler,实现表演接口
- package com.jCuckoo.demo;
- public class Juggler implements Performer {
- private int beanBags = 3;
- public Juggler() {
- }
- public Juggler(int beanBags) {
- this.beanBags = beanBags;
- }
- public void perform() throws Exception {
- System.out.println("表演开始:魔术师欺骗了" + beanBags + "个游戏豆。");
- }
- }
spring的配置文档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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <bean id="juggler" class="com.jCuckoo.demo.Juggler"/>
- <bean id="audience" class="com.jCuckoo.demo.Audience" />
- <aop:config>
- <aop:aspect ref="audience">
- <aop:pointcut id="performance"
- expression="execution(* com.jCuckoo.demo.Performer.perform(..))" />
- <aop:before pointcut-ref="performance" method="takeSeats" />
- <aop:before pointcut-ref="performance" method="turnOffCellPhones" />
- <aop:after pointcut-ref="performance" method="turnOnCellPhones" />
- <aop:after-returning pointcut-ref="performance"
- method="applaud" />
- <aop:after-throwing pointcut-ref="performance"
- method="demandRefund" />
- <aop:around pointcut-ref="performance" method="watchPerformance"/>
- </aop:aspect>
- </aop:config>
- </beans>
测试类,获取魔术师,并进行表演。
- package com.jCuckoo.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.jCuckoo.demo.Performer;
- public class MainTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
- Performer performer=(Performer)ctx.getBean("juggler");
- try {
- performer.perform();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
最终结果:
----开演之前,请占座----
----开始之前,请关机----
oooooooo环绕通知开始oooooooo
表演开始:魔术师欺骗了3个游戏豆。
****演出结束,可以开机****
****鼓掌,继续鼓掌。****
oooooooo环绕通知结束oooooooo
演出耗时共计:1毫秒。
Spring3.0中的前置通知、后置通知、环绕通知、异常通知相关推荐
- spring的几个通知(前置、后置、环绕、异常、最终)
1.没有异常的 2.有异常的 1.被代理类接口Person.java 1 package com.xiaostudy; 2 3 /** 4 * @desc 被代理类接口 5 * 6 * @author ...
- 前置,后置,环绕,异常增强(示例)
----------------------------------基础架构: 前置增强 ------------------------------------代码演示: public interf ...
- c++中的前置和后置加加没你想的这么简单
1.前置++和后置++的区别1: 前置是先加后赋值,后置是先赋值再加. 2.前置++比后置++运行的速度快: 在汇编底层中后置++会比前置++多一行. 当然我这demo不是很准确毕竟也是有偶然的,但是 ...
- Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)
AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...
- python测试框架之Pytest(二) : 测试用例的前置setup后置teardown用法
目录 pytest中的前置和后置 类内外执行:方法细化级setup/teardown 类外执行:模块级(setup_module/teardown_module) 类外执行:函数级(setup_fun ...
- cad怎么将图层后置_CAD中如何将图形前置和后置吗? - CAD自学网
如果图中都是线性图形且最终要单色打印,通常不用太理会图形的顺序.但如果实体填充且与其他图形有重叠,就需要设置图形的顺序了,有时我们在插入一些设备或构件时,为了图面更整洁,会在图块中添加区域覆盖WIPE ...
- cad怎么将图层后置_CAD中如何将图形前置和后置吗?
如果图中都是线性图形且最终要单色打印,通常不用太理会图形的顺序.但如果实体填充且与其他图形有重叠,就需要设置图形的顺序了,有时我们在插入一些设备或构件时,为了图面更整洁,会在图块中添加区域覆盖WIPE ...
- PHP通过__call实现简单的AOP(主事务后的其他操作)比如前置通知,后置通知
/*** person class*/ class Person {/*** person class -> function say*/public static function say($ ...
- # c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符...
c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符 标签(空格分隔): c++ 前言 我在c++学习的过程中, 对这几个不太常见的运算符重载不太会写.出现了很 ...
最新文章
- 多媒体计算机系统是能进行获取,第六章 7 多媒体计算机系统.pdf
- [Django] 查看orm自己主动运行的原始查询sql
- cassandra可视化工具_程序员绘图工具——PlantUML
- String,StringBuffer,StringBuilder
- 编程关键词介绍...
- python中if语句使用_如何在python中使用'空if语句'?
- GNN上用到的Tasks,Dataset and Benchmark
- spring-boot-starter-parent的主要作用
- 黑大选修计算机模拟物理学,黑龙江大学学分制选课指南.doc
- 【前端 · 面试 】HTTP 总结(四)—— HTTP 状态码
- Android蓝牙服务
- java模拟时钟课程设计_Java课程设计时钟图形模拟
- QT自定义3D图形控件,支持轴拖动。
- Web前端开发技术实验与实践(第3版)储久良编著实训11
- winform程序:newtonsoft json 序列化时出现 “unterminated string. Excepted delimiter...
- 433MHz,2.4GHz,GPRS,NB-IOT各有哪些特点?
- 同源性 相似性 一致性
- (转)2007北京高考作文题目(太有才了.我都看了无数遍了)
- C++课程设计快递业务管理系统实验报告
- 构成社群的五要素,你了解吗?
热门文章
- hdu2438 三分
- 【错误记录】Android Gradle 配置报错 ( gradle.properties 配置到 BuildConfig 中需要注意类型转换 | 位置: 类 BuildConfig )
- 大数据笔记(六)——HDFS的底层原理:JAVA动态代理和RPC
- bzoj 3223: Tyvj 1729 文艺平衡树
- 【eoe教程】Android中自定义视图的绘制方法
- servlet中的几个路径有关的方法
- 从JVM看类的加载过程与对象实例化过程
- 深入理解C/C++二维数组
- 如何玩转跨库Join?跨数据库实例查询应用实践
- 工信部部长苗圩于CITE发表致辞,指引三个方向推动国内电子信息产业持续发展...