AOP为Aspect Oriented Programming的缩写, 意为:面向切面编程,主要是使各部分之间的耦合度降低, 提高程序的可重用性, 同时提高了开发的效率。
2. AOP的作用及优势
在程序运行期间,不修改源码对已有方法进行增强(加上统一的逻辑处理)。
Joinpoint(连接点) :就是根据规则,可以指定拦截的方法,我们将每一个被拦截的方法称为连接点
Pointcut(切入点) : 所谓切入点就是拦截方法设置的规则
Advice(通知/增强,场景:在之前拦截,还是方法执行后拦截呢?)
通知就是可以设置在方法之前拦截或者方法执行之后拦截或者方法出异常后拦截,或者方法之前和之后都拦截。我们将这些拦截场景称为通知
4. 基于xml配置aop
4.1.第一步加入aop需要的包
Spring的AOP包基于AspectJ框架,所以必须加入AspectJ-->aspectjweaver.jar
import org.aspectj.lang.ProceedingJoinPoint;
System.out.println("-before-方法执行之前-");
System.out.println("-after-方法执行之后-");
//around需要一个ProceedingJoinPoint对象指定方法调用之前后之后
public void around(ProceedingJoinPoint point) {
System.out.println("-around-在方法之前后执行-");
System.out.println("-around-在方法之后执行-");
public void afterThrowing() {
System.out.println("-afterThrowing-出了异常后执行");
public void afterReturning() {
System.out.println("-afterReturning-方法执行成功之后-");
4.3、配置AOP配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean name="now" class="java.util.Date"></bean>
<bean name="studentService" class="com.sxt.service.impl.SutdentServiceImpl">
<bean name="aop" class="com.sxt.aop.Aop"></bean>
<!-- aop:config: 用于设置AOP拦截的跟标签 -->
<!--aop:aspect标签 指定AOP的切面,以及拦截规则对应的方法 -->
<aop:aspect id="aop-aspect" ref="aop">
<!-- 拦截的方法必须不出异常,才在方法执行之后加入切面逻辑代码 -->
<aop:after-returning method="afterReturning" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
<!-- 拦截的方法必须出了异常,才在方法执行之后加入切面逻辑代码 -->
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
<!-- 拦截的方法不管有没有异常,可以实现在拦截方法之前和之后分别加入不同的逻辑代码 -->
<aop:around method="around" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())" />
<!-- 拦截的方法不管有没有异常,可以实现在拦截方法之前加入切面的逻辑代码 -->
<aop:before method="before" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
<!-- 拦截的方法不管有没有异常,可以实现在拦截方法之后加入切面的逻辑代码 -->
<aop:after method="after" pointcut="execution(* com.sxt.service.impl.SutdentServiceImpl.*())"/>
4.4. 切入点表达式说明
表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
public void cn.gzsxt.service.impl.CustomerServiceImpl.saveCustomer()
void cn.gzsxt.service.impl.CustomerServiceImpl.saveCustomer()
* cn.gzsxt.service.impl.CustomerServiceImpl.saveCustomer()
包名可以使用*号,表示任意包,但是有几级包,需要写几个*
* *.*.*.*.CustomerServiceImpl.saveCustomer()
* com..CustomerServiceImpl.saveCustomer()
参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数
参数列表可以使用..表示有无参数均可,有参数可以是任意类型
5.1. <aop:config
5.2. <aop:aspect>
5.3. <aop:pointcut>
5.4. <aop:before>
5.5. <aop:after-returning>
用于配置后置通知,如果出了异常就一定不会调用切面的方法
5.6. <aop:after-throwing>
用于配置异常通知,只有出了异常才会调用切面对应的方法
5.7. <aop:after>
用于配置最终通知,不管出不出异常,调用的切面的方法
5.8. <aop:around>
- Spring框架中的AOP技术----配置文件方式
1.AOP概述 AOP技术即Aspect Oriented Programming的缩写,译为面向切面编程.AOP是OOP的一种延续,利用AOP技术可以对业务逻辑的各个部分进行隔离,从使得业务逻辑各部 ...
- Spring框架中的AOP技术----注解方式
利用AOP技术注解的方式对功能进行增强 CustomerDao接口 package com.alphajuns.demo1; public interface CustomerDao { public ...
- Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束
(aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...
- Spring框架中的aop操作之二 通过配置文件实现增强
aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- Spring Security框架中踢人下线技术探索
1.背景 在某次项目的开发中,使用到了Spring Security权限框架进行后端权限开发的权限校验,底层集成Spring Session组件,非常方便的集成Redis进行分布式Session的会话 ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- (转)Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
随机推荐
- cookie用法小结 cookie.setPath 跨域共享
1. JSP中Cookie的读写 Cookie的本质是一个键值对,当浏览器访问web服务器的时候写入在客户端机器上,里面记录一些信息.Cookie还有一些附加信息,比如域名.有效时间.注释等等. 下面 ...
- Spring框架总结(十)
XML方式实现AOP编程 Xml实现aop编程: 1) 引入jar文件 [aop 相关jar, 4个] 2) 引入aop名称空间 3)aop 配置 * 配置切面类 (重复执行代码形成的类) * aop ...
- 编写高质量代码改善C#程序的157个建议——建议86:Parallel中的异常处理
建议86:Parallel中的异常处理 建议85阐述了如何处理Task中的异常.由于Task的Start方法是异步启动的,所以我们需要额外的技术来完成异常处理.Parallel相对来说就要简单很多,因 ...
- 使用 console.time() 计算js代码执行时间
console.time('hellor'); for(var i=0;i<100000;i++){} console.timeEnd('hellor');
- Eclipse的Debug调试技巧大全
转载 原文链接:https://blog.csdn.net/u011781521/article/details/55000066 收藏方便以后查看. 19:18:10 2018-12-29
- java中介者模式
中介者模式也是用来降低类类之间的耦合的,因为如果类类之间有依赖关系的话,不利于功能的拓展和维护,因为只要修改一个对象,其它关联的对象都得进行修改.如果使用中介者模式,只需关心和Mediator类的关系 ...
- 在 Mac OSX 上安装 nginx
今天在使用 brew 安装 nginx 时,提示错误,安装不上去: brew install nginx, 提示:/usr/local is not writable. 这个是需要修改 /usr/lo ...
- windows 多个人同时远程同一台电脑
windows 多个人同时远程同一台电脑 第一步:(内外远程) 参考内网多个人同时远程一台电脑: http://www.cnblogs.com/zlp520/p/7688984.html 第二步:( ...
- 「HNOI 2015」亚瑟王
\(Description\) 有\(n\)张卡牌,每一张卡牌有\(p_i\)的概率发动,并造成\(d_i\)点伤害.一共有\(r\)轮,每一轮按照编号从小到大依次考虑,如果这张牌已经发动过则跳过该牌 ...
- Subway Pursuit (二分)(交互题)
题目来源:codeforces1039B Subway Pursuit 题目大意: 在1到n里有一个运动的点,要求找到这个点,每次可以查询一个区间内有没有这个点,每次这个点往左或者往右移动1到k个位置 ...