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的缩 ...
随机推荐
- 在IIS和Nginx上通过代理部署基于ant-design-pro前端框架开发的应用
一.本文解决的主要问题 通过对前端静态资源站点进行代理服务设置,实现对后端API接口的代理,从而实现前端的独立部署,即通过代理的设置实现对http://IP0:Port0/api/xxx的请求转发至h ...
- C#6.0特性(快来围观)(转)
出处:http://www.cnblogs.com/HJL-Blog/p/4457632.html 说明一下,很多博友一进来就认为仅仅是语法糖,C#语法的更新,代表着它的进步,语法糖是为了让我们更好的 ...
- C# SendKeys用法
功能:将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样. 语法:SendKeys.Send(string keys);SendKeys.SendWait(string keys); 说明 ...
- NetBeans找不到C/C++编译器
如果您已经安装 NetBeans IDE 6.9,但其中不包括 C/C++ 插件 如果在选择“文件”>“新建项目”时,NetBeans IDE 未显示 "C/C++" 项目类 ...
- 洛谷P1501 [国家集训队]Tree II(打标记lct)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- Freemark与spring整合
SpringMVC环境的搭建在这里就不多说了,我们这节主要是FreeMarker与SpringMVC整合 首先,在springmvc的配置文件普通视图之前,加入freemarker的视图 fre-se ...
- rabbitmqBat常用指令
激活 RabbitMQ's Management Pluginrabbitmq-plugins.bat enable rabbitmq_management 查看已有用户及用户的角色rabbitmqc ...
- [Mac] 获取cpu信息
[Mac] 获取cpu信息 命令行获取cpu信息 sysctl machdep.cpu output like machdep.cpu.tsc_ccc.denominator: 0 machdep.c ...
- select2 模糊查询远程数据
详细:http://www.cnblogs.com/linJie1930906722/p/6060370.html $("#name").select2({ language: & ...
- Delphi XE8帮助中的REST相关内容。
Delphi XE8的离线帮助是我见过的最好的Delphi帮助文档了,内容相当详细和丰富,几乎涵盖了Delphi的方方面面!! Delphi XE8的帮助文档在哪里?“XE8安装目录\Help\Doc ...