spring框架应用系列四:切面编程(环绕通知与前后置通知区别)
切面编程(环绕通知与前后置通知区别)
本文系作者原创,转载请注明出处: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中相应的方法,达到通知的效果;
应用实例:观众观看表演所做出的相应行为
先列出相关接口以及类代码
节目表演接口(切点方法)
package com.spring.example.aspectAround; /**
* Created by weixw on 2017/11/16.
*/
public interface Performer { void perform();
}
切点类实现接口Juggler
package com.spring.example.aspectAround; import org.springframework.stereotype.Component; /**
* Created by weixw on 2017/11/16.
*/
@Component
public class Juggler implements Performer {
private int beanBags = 3;
public Juggler(){ }
public Juggler(int beanBags){
this.beanBags = beanBags ;
}
@Override
public void perform() {
System.out.println("JUGGLING "+ beanBags + " BEANBAGS");
try {
Thread.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
} }
上述代码都能共用,下面分别列举前后置通知与环绕通知区别代码
前后置通知(通过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即可
package com.spring.example.aspectAround;/**
* Created by weixw on 2017/11/16.
*/ import javafx.application.Application;
import javafx.stage.Stage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Driver extends Application { public static void main(String[] args) {
launch(args);
} @Override
public void start(Stage primaryStage) {
try { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/aspect-around.xml");
Performer performer = (Performer) ctx.getBean("juggler");
performer.perform(); }catch (Exception e){
e.printStackTrace();
}
}
}
运行结果
环绕通知结果:
前后置通知结果:
总结
上述列出前后置通知和环绕通知样例。对于有变量缓存需求,线程安全的应用场景,前后置通知实现比较困难,而环绕通知实现就非常容易;
不要让懒惰占据你的大脑,不要让妥协拖垮你的人生。青春就是一张票,能不能赶上时代的快车,你的步伐掌握在你的脚下。
spring框架应用系列四:切面编程(环绕通知与前后置通知区别)的更多相关文章
- Spring实战4:面向切面编程
主要内容 面向切面编程的基本知识 为POJO创建切面 使用@AspectJ注解 为AspectJ的aspects注入依赖关系 在南方没有暖气的冬天,太冷了,非常想念北方有暖气的冬天.为了取暖,很多朋友 ...
- Spring框架的第四天(整合ssh框架)
## Spring框架的第四天 ## ---------- **课程回顾:Spring框架第三天** 1. AOP注解方式 * 编写切面类(包含通知和切入点) * 开启自动代理 2. JDBC模板技术 ...
- Spring Boot之AOP面向切面编程-实战篇
目录 前言 编程范式主要有以下几类 引入pom依赖 aop注解 实现日志分割功能 前言 AOP是一种与语言无关的程序思想.编程范式.项目业务逻辑中,将通用的模块以水平切割的方式进行分离统一处理,常用于 ...
- Spring AOP前置通知和后置通知
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...
- spring框架应用系列三:切面编程(带参数)
本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7786715.html 解决问题 1.分离业务监控与业务处理.简单点 ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- Spring(三)面向切面编程(AOP)
在直系学长曾经的指导下,参考了直系学长的博客(https://www.cnblogs.com/WellHold/p/6655769.html)学习Spring的另一个核心概念--面向切片编程,即AOP ...
- Spring中AOP简介与切面编程的使用
Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...
- 02 浅析Spring的AOP(面向切面编程)
1.关于AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
随机推荐
- python+matplotlib+绘制不同图标类型
#==================================================================#首先需要导入两个文件import matplotlib.pypl ...
- C语言中free()函数释放struct结构体中的规律
并不是什么新鲜的事情,不过值得注意.首先我们知道,在使用struct来定义并声明一个变量时,将会自动划分出一个连续的储存空间(虽然根据某些对齐原则会出现内存间隙,但是大体上来说还是连续的)这一块连续空 ...
- 【C语言编程练习】5.11 猴子吃桃子问题
1. 问题描述 有一只猴子,第一天摘下若干个桃子,当即吃掉了一半,又多吃了一个,第二天又将剩下的桃子吃掉了一半,又多吃了一个,按照这样的吃法,每天都吃前一天吃下的桃子的一半又一个.到第十天,就剩下了一 ...
- ubuntu 14.04 安装 rabbitmq
1. sudo apt-get update 安装rabbitmq 2. sudo apt-get install rabbitmq-server 添加用户 3. sudo rabbitmqctl a ...
- 查找更改的PeopleCode
当我们做工程包迁移时,经过会遗漏部分更改过的定义.我们可以用下面的SQL来查找变更项 变量 &OPRID =代码变更者 变量 &PROJECT 项目工程名 SELECT * FROM ...
- 微信小程序之canvas绘制海报分享到朋友圈
绘制canvas内容 首先,需要写一个canvas标签,给canvas-id命名为shareBox <canvas canvas-id="shareBox"></ ...
- 第一次冲刺意见汇总&团队第一阶段总结
大家对我们小组的意见基本是: 1.设计界面简单 2.功能较少 3.没有实现切换歌曲的功能 谢谢HT小组的走心评价 接下来我们组内准备:1.先调节用户界面,插入一些图片,美化界面,给用户直观的体验上升. ...
- 自己封装element-ui树组件的过滤
前言:vue开发项目时用到了element-ui的树组件,但是发现一执行过滤事件,树就全部都展开了,为了解决这个问题,只能自己先过滤数剧,再赋值给树组件的data,就避免了一上来全部展开的尴尬. 一. ...
- Hadoop集群搭建-full完全分布式(三)
环境:Hadoop-2.8.5 .centos7.jdk1.8 一.步骤 1).4台centos虚拟机 2). 将hadoop配置修改为完全分布式 3). 启动完全分布式集群 4). 在完全分布式集群 ...
- hive-issue-inserting-records-to-partitioned-table
hive-issue-inserting-records-to-partitioned-table Hi Sam, Recently we upgraded our cluster from HDP2 ...