切面编程(环绕通知与前后置通知区别)
解决问题
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
28 }
上述代码都能共用,下面分别列举前后置通知与环绕通知区别代码
前后置通知(通过AspectJ注解实现,注意:<aop:aspectj-autoproxy/>不能少,它实现了切面相关方法绑定在切点上,切点方法执行就能触发相应通知)
XML配置文件:spring/aspect-aspectJnoArgs.xml(放在spring文件夹下)
<?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:context="http://www.springframework.org/schema/context"
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">
<!--使用前置通知和后置通知唯一方式:在前置通知中记录开始时间,并在后置通知中报告表演耗费的时长,必须保存开始时间。因为Audience是单例,如果像这样保-->
<!--存状态,会存在线程安全问题;-->
<context:component-scan base-package="com.spring.example.aspectAspectJNoArgs"/>
<aop:aspectj-autoproxy/>
</beans>
前后置通知切面实现类
package com.spring.example.aspectAspectJNoArgs;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* Created by weixw on 2017/11/16.
* 通过AspectJ注解实现切面编程
* 切点方法 id 默认是所依赖方法(public void performance(){})的小写方法名performance
*/
@Component
@Aspect
public class Audience {
@Pointcut("execution(* com.spring.example.aspectAspectJNoArgs.Performer.perform(..))") //定义切点
public void performance(){}
@Before("performance()")//表演之前
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
@Before("performance()")//表演之前
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones.");
}
@AfterReturning("performance()")//表演之后
public void applaud(){
System.out.println("CLAP CLAP CLAP CLAP CLAP ");
}
@AfterThrowing("performance()") //表演失败之后
public void demandRefund(){
System.out.println("Boo! We want our money back!");
}
}
环绕通知
XML配置文件:spring/aspect-around.xml(放在spring文件夹下)
<?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:context="http://www.springframework.org/schema/context"
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">
<!--前置通知和后置通知是在一个方法中实现,所以不需要保存变量值,自然是线程安全的;-->
<context:component-scan base-package="com.spring.example.aspectAround"/>
<!--通过component-scan自动扫描,@Component注解将Magician注册到spring容器-->
<aop:config>
<!--audience :切面 watchPerformance:切面方法 performance:切点-->
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* com.spring.example.aspectAround.Performer.perform(..))"/>
<aop:around pointcut-ref="performance" method="watchPerformance" />
</aop:aspect>
</aop:config>
</beans>
环绕通知切面实现类
package com.spring.example.aspectAround;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/**
* Created by weixw on 2017/11/16.
*/
@Component
public class Audience {
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones.");
}
public void applaud(){
System.out.println("CLAP CLAP CLAP CLAP CLAP");
}
public void demandRefund(){
System.out.println("Boo! We want our money back!");
}
public void watchPerformance(ProceedingJoinPoint joinPoint){
try{
takeSeats(); //表演之前
turnOffCellPhones(); //表演之前
long start = System.currentTimeMillis();
System.out.println("The performance start ......");//节目开始
joinPoint.proceed(); //执行被通知的方法
System.out.println("The performance end ......");//节目结束
long end = System.currentTimeMillis(); //表演之后
applaud();//表演之后
System.out.println("The performance took milliseconds:"+ (end - start) );//表演时长
}catch (Throwable t){
demandRefund(); //表演失败之后
}
}
}
测试代码
环绕通知测试代码如下,前后置通知测试代码只需将配置文件名称改成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
21 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/aspect-around.xml");
22 Performer performer = (Performer) ctx.getBean("juggler");
23 performer.perform();
24
25 }catch (Exception e){
26 e.printStackTrace();
27 }
28 }
29 }
运行结果
环绕通知结果:
前后置通知结果:
总结
上述列出前后置通知和环绕通知样例。对于有变量缓存需求,线程安全的应用场景,前后置通知实现比较困难,而环绕通知实现就非常容易;
- 稳扎稳打JavaScript(一)——作用域链内存模型
- 稳扎稳打JavaScript(二)——图解对象内存模型
- Swift学习资源
- 稳扎稳打JavaScript(三)——创建对象的几种方式
- 快速教程:使用Cython来扩展Python/NumPy库
- 稳扎稳打JavaScript(四)——闭包
- JavaScript奇淫技巧(一)
- 动态规划法(四)——0/1背包问题
- 动态规划法(三)——最长公共子序列
- Python数据分析模块 | pandas做数据分析(三):统计相关函数
- 动态规划法(二)——弗洛伊德算法
- Python数据分析模块 | pandas做数据分析(二):常用预处理操作
- 贪心算法(一)——概述
- Python数据分析模块 | pandas做数据分析(一):基本数据对象
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- vue简单实现九宫格抽奖
- [已解决]报错:xlrd.compdoc.CompDocError: Workbook: size exceeds expected 17920 bytes; corrupt?
- [已解决]报错:have mixed types. Specify dtype option on import or set low_memory=False
- Mysql删除表数据,表文件大小不变
- 抓取模板
- 利用os和pandas来合并当前目录下所有excel文件
- Python科学计算:用NumPy快速处理数据
- Python中过滤HTML标签的函数
- 词云图展示
- 将xml文件转为txt文件
- 数据概览神器pandas_profiling
- [未完]构建代理池
- ClickHouse入门实例:样例数据Yandex.Metrica
- Python连接redis
- JavaScript去除数组中重复的数字