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的缩 ...
随机推荐
- CSS实现input默认文字灰色有提示文字点击后消失鼠标移开显示
CSS实现input美化操作默认是为灰色,并且有提示 如下图 鼠标点击后文字消失,鼠标移开后文字显示 给input入下图添加代码 style="color:#cccccc; outline: ...
- ETC系统简介
ETC:电子不停车系统 主要由两部分构成:OBU(车载单元,又叫电子标签)和RSU(路基单元,包括天线) 其中OBU里插有用户卡(一般是和银行联名发行的信用卡) 而RSU包括路基天线,PSAM卡,通过 ...
- Android动态加载--JVM 类加载机制
动态加载,本质上是通过JVM类加载机制将插件模块加载到宿主apk中,并通过android的相关运行机制,实现插件apk的运行.因此熟悉JVM类加载的机制非常重要. 类加载机制:虚拟机把描述类的数据从C ...
- [GO]go语言实现区块链工作证明(pow)原理
package main import ( "math/big" "bytes" "math" "crypto/sha256&qu ...
- java中Integer常量池
我们先看一个关于Integer的例子 public static void main(String[] args) { // TeODO Auto-generated method stu Integ ...
- java中的上转型解释(多态的另一种)
我们先来看个例子: public class Polymorphism extends BaseClass{ public String book="轻量级j2ee教程"; pub ...
- mybaits foreach
<select id="selectQuickConsultDoctorList" resultMap="BaseResultMap" parameter ...
- 8) Struts2 2 SpringMVC
git@github.com:witaste/smse.git 数据库脚本: /* Navicat MySQL Data Transfer Source Server : 新服务器 Source Se ...
- 团体程序设计天梯赛L2-003 月饼 2017-03-22 18:17 42人阅读 评论(0) 收藏
L2-003. 月饼 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不 ...
- NSArray去除重复元素
直接上代码吧! 1.可以创建一个新的数组,对需要去除重复的数组进行遍历,如果新数组不包含就数组,那么添加元素,如果包含就不添加. NSMutableArray *array = [NSMutableA ...