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的缩 ...
随机推荐
- 【转载】java实现rabbitmq消息的发送接受
原文地址:http://blog.csdn.net/sdyy321/article/details/9241445 本文不介绍amqp和rabbitmq相关知识,请自行网上查阅 本文是基于spring ...
- BZOJ 2243 染色 (线段树+树链剖分)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 9895 Solved: 3735[Submit][Status ...
- webapi 跨域访问设置基于jsonp跨域
JSONP实现跨域 Web API并没有提供JSONP Formatter,但是这并不能影响我们前进的脚步,我们可以自定义Formatter来实现JSONP功能.既然是利用JSONP跨域,那么就得简 ...
- STL 中的map 与 hash_map的理解
可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...
- Xshell 连接Linux虚拟机失败解决办法
当我们在VMWare上安装好Ubuntu虚拟机后,有时候会需要使用多个terminal窗口,但是在虚拟机中多个窗口互相切换查看不方便,这时候用物理机的xshell工具连接到虚拟机中去就显得很方便.直接 ...
- Mysql主主复制+keepalived
1>环境 Master1 10.0.0.201 Master2 10.0.0.202 2>Master1操作授权 mysql>grant replication slave on * ...
- New Year, New Devs: Sharpen your C# Skills
At the beginning of each new year, many people take on a challenge to learn something new or commit ...
- leetcode 反转字符串
请编写一个函数,其功能是将输入的字符串反转过来. 示例: 输入:s = "hello" 返回:"olleh" /** * @param {string} s * ...
- EF:无法检查模型兼容性,因为数据库不包含模型元数据。
Model compatibility cannot be checked because the database does not contain model metadata. Model ...
- hdu Surround the Trees
题目链接:戳我 凸包模板 #include<iostream> #include<cstdio> #include<cstring> #include<alg ...