Spring中AOP学习笔记
AOP
- 描述(摘抄百度百科)
- AOP开发中的相关操作术语
- 实例(xml方式)
- 通知的类型
- 切入点表达式的写法:
- 实例(注解方式)
描述(摘抄百度百科)
AOP(面向切面编程):Aspect Oriented Programming的缩写。意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP开发中的相关操作术语
Joinpoint(连接点):所谓连接点是指那些被拦截到的点。其实就是类里面可以被增强的方法,这些方法称为连接点。
Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。比如实际开发中只对saveAccount()方法增强,该方法就是切入点。
Advice(通知/增强):增强的逻辑,称为增强或通知
Aspect(切面):把增强应用到切入点上面的过程称为切面。
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
Target(目标对象):代理的目标对象。
Proxy(代理):代理对象
Weaving(织入):将通知应用到目标的过程。例如:将日志记录应用到saveAccount()方法上面。
下图更直观:
实例(xml方式)
创建一个meavn工程,pom文件中添加如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
AccountService 接口:
public interface AccountService {
/***
* 保存账户
*/
void saveAccount();
/***
* 更新账户
*/
void updateAccount(Integer i);
/***
* 删除账户
*/
void deleteAccount(Integer i);
}
AccountServiceImpl实现类:
public class AccountServiceImpl implements AccountService {
public void saveAccount() {
System.out.println("保存用户成功.....");
}
public void updateAccount(Integer i) {
System.out.println("更新用户成功.....");
}
public void deleteAccount(Integer i) {
System.out.println("删除用户成功.....");
}
}
再编写一个增强的类Logger:
public class Logger {
/***
* @Description: 用于打印日志
* @Date: 2021/2/9
*/
public void Printlog(){
System.out.println("输出打印日志.....");
}
}
bean.xml配置:
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置service-->
<bean id="accountService" class="com.zy.aop.service.AccountServiceImpl"></bean> <!--配置切面logger类-->
<bean id="logger" class="com.zy.aop.logger.Logger"></bean> <!--配置Aop-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="log" expression="execution(public void com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<!--配置切面-->
<aop:aspect id="" ref="logger">
<!--前置通知:在切入点方法执行前执行-->
<aop:before method="Printlog" pointcut-ref="log"></aop:before>
</aop:aspect>
</aop:config>
</beans>
测试代码:
public class TestAop {
@Test
public void TestSaveAccount(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
accountService.saveAccount();
accountService.updateAccount(1);
Integer i = 10;
}
}
运行结果:
标签说明:
| 标签 | 说明 |
|---|---|
| aop:config | 该标签声明aop配置开始 |
| aop:pointcut | 用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。属性:expression:用于定义切入点表达式。id:用于给切入点表达式提供一个唯一标识 |
| aop:aspect | 用于配置切面。属性:id:给切面提供一个唯一标识。ref:引用配置好的通知类 bean 的 id |
通知的类型
| 通知类型 | 标签 |
|---|---|
| 前置通知 | <aop:before method="" pointcut-ref=""></aop:before> |
| 后置通知 | <aop:after-returning method="" pointcut-ref=""></aop:after-returning> |
| 异常通知 | <aop:after-throwing method=""></aop:after-throwing> |
| 最终通知 | <aop:after method="" pointcut-ref=""></aop:after> |
| 环绕通知 | <aop:around method="" pointcut-ref=""></aop:around> |
每一个标签中都有这三个属性method:指定通知中方法的名称。pointct:定义切入点表达式pointcut-ref:指定切入点表达式的引用。
前置通知:
<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--前置通知:在切入点方法执行前执行-->
<aop:before method="Printlog" pointcut-ref="log"></aop:before>
</aop:aspect>
</aop:config>
后置通知:
<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--后置通知:在切入点方法执行后执行-->
<aop:after-returning method="Printlog" pointcut-ref="log"></aop:after-returning>
</aop:aspect>
</aop:config>
异常通知:
<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--异常通知:程序出现异常的时候,进行的操作-->
<aop:after-throwing method="Printlog" pointcut-ref="log"></aop:after-throwing>
</aop:aspect>
</aop:config>
最终通知:
<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--最终通知:在切入点方法执行后执行,无论方法是否正常执行都会在后面执行-->
<aop:after method="Printlog" pointcut-ref="log"></aop:after>
</aop:aspect>
</aop:config>
环绕通知:
<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--环绕通知-->
<aop:around method="Printlog2" pointcut-ref="log"></aop:around>
</aop:aspect>
</aop:config>
我们要修改一下增强的类Logger中的方法:
public class Logger {
/***
* @Description: 用于打印日志
* @Date: 2021/2/9
*/
public void Printlog() {
System.out.println("输出打印日志.....");
}
public Object Printlog2(ProceedingJoinPoint pro) {
Object proceed = null;
Object[] args = pro.getArgs();//得到方法实行所需参数
try {
//在这之前是前置通知
proceed = pro.proceed(args);//调用业务层方法
//在这之后是后置通知
return proceed;
} catch (Throwable t) {
//在这是异常通知
} finally {
//在这是最终通知
}
return proceed;
}
}
切入点表达式的写法:
表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
写法说明:
全匹配方式:public void com.zy.aop.service.AccountServiceImpl.saveAccount(参数类型)
访问修饰符可以省略: void com.zy.aop.service.AccountServiceImpl.saveAccount(参数类型)
返回值可以使用号,表示任意返回值: com.zy.aop.service.AccountServiceImpl.saveAccount(参数类型),具体可以百度在这我就不写了。
通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。
execution(* com.zy.aop.service..(…))
Spring中AOP学习笔记的更多相关文章
- spring 中bean学习笔记
spring 中bean 一.bean的定义和应用 1. bean 形象上类似于getXX()和setXX()的一种. 2. 由于java是面向对象的,类的方法和属性在使用中需要实例化. 3. 规律: ...
- Spring中AOP原理,源码学习笔记
一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...
- Spring入门IOC和AOP学习笔记
Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容 ...
- Spring 源码学习笔记10——Spring AOP
Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点
Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...
- Spring中AOP简介与切面编程的使用
Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...
- spring cloud(学习笔记)高可用注册中心(Eureka)的实现(二)
绪论 前几天我用一种方式实现了spring cloud的高可用,达到两个注册中心,详情见spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一),今天我意外发现,注册中心可以无限 ...
随机推荐
- 【Linux】fstab中 每个字段代表的含义
默认情况下,fstab中已经有了当前的分区配置,内容可能类似: # <file system> <mount point> <type> <options ...
- 【Linux】linux rinetd 端口转发部署
linux下简单好用的工具rinetd,实现端口映射/转发/重定向 Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具.Rinetd是单一过程的服务器,它 ...
- WPF NET5 Prism8.0的升级指南
前言 曾经我以学习的目的写了关于在.NET Core3.1使用Prism的系列文章.NET Core 3 WPF MVVM框架 Prism系列文章索引,也谢谢大家的支持,事实上当初的版本则是Pri ...
- Gulp4.0入门和实战
gulp4.0入门和实战 我最近遇到需要优化web的性能的任务,然后就捣鼓了一些对资源文件优化压缩的方案.由于之前的项目中有使用到gulp,所以在需要处理的web项目中也优先使用这个技术. 先聊聊gu ...
- MySQL库和表的操作
MySQL库和表的操作 库操作 创建库 1.1 语法 CREATE DATABASE 数据库名 charset utf8; 1.2 数据库命名规则 可以由字母.数字.下划线.@.#.$ 区分大小写 唯 ...
- 记录list.remove()和list.pop()
list.remove(obj):这个是移除列表中某个值的第一个匹配项 list.pop(index):这个是移除列表中下标为index的元素 当元素全是数字或者有数字时注意区分.
- Android N wifi auto connect流程分析
定义 当有两个或者两个以上的已经保存的无线网络可以连接时,系统通过选择算法来选择一个最优网络. 在Android L,wifi的自动重连机制是由WifiAutoJoinController 类来实现, ...
- 设置一个两边固定中间自适应的css
1.两边浮动,中间自动宽度 给左右两个盒子设置左右浮动,中间的盒子不设置宽度,左右两边边距为左右盒子的宽度,中间盒子的位置必须写在右盒子下面,不然会把右盒子挤下去 如: <div class ...
- 日志采集技术分析 Inode Inotify
日志采集技术分析[阿里] - 新手学习导向 - 中国红客联盟 - Powered by HUC http://www.cnhonkerarmy.com/thread-236973-1-1.html
- 如何让淘宝不卡顿? 读写比例 动态扩容 分布式化路线 mysql 优化
作为数据库核心成员,如何让淘宝不卡顿? https://mp.weixin.qq.com/s/l-qXV8NI6ywnUvp3S6an3g