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)的实现(一),今天我意外发现,注册中心可以无限 ...
随机推荐
- 使用K8s的一些经验和体会
坑 Java应用程序的奇怪案例 在微服务和容器化方面,工程师倾向于避免使用 Java,这主要是由于 Java 臭名昭著的内存管理.但是,现在情况发生了改变,过去几年来 Java 的容器兼容性得到了 ...
- C/C++内存对齐详解
1.什么是内存对齐 还是用一个例子带出这个问题,看下面的小程序,理论上,32位系统下,int占4byte,char占一个byte,那么将它们放到一个结构体中应该占4+1=5byte:但是实际上,通过运 ...
- SDUST数据结构 - 19级期末考试
判断题: 选择题: 函数题: 6-1 统计二叉树叶子结点个数: 题目: 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> typ ...
- Centos7.4 小白式安装(初学)
虚拟机安装Centos7.4系统 适用人群(初学者) 下载Centos7.4镜像 https://pan.baidu.com/s/1NtjfdHV3OWAvfDj5vrR7HQ 提取码:hzzw 虚 ...
- TCP服务器程序
Linux下编写TCP服务器调用的函数顺序为:socket -> bind -> listen -> accept -> recv/send socket 参见:http:// ...
- Mybatis报错:Could not find resource mybatis-conf.xml
Mybatis报错:Could not find resource mybatis-conf.xml 报错截图: 报错内容: java.io.IOException: Could not find r ...
- 【python刷题】LRU
什么是LRU? LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰.该算法赋予每个页面一个访问字段,用来记录一个页面自上次 ...
- Vim中的swp文件,在vim非正常退出时,再次编辑会出问题
vim中的swp即swap文件,在编辑文件时产生,它是隐藏文件,如果原文件名是data,那么swp文件名就是.data.swp.如果文件正常退出,则此文件自动删除.以下两种情况不会删除swp文件: V ...
- windows IOCP 实践
关于 windows IOCP 有人说 windows IOCP 是 windows 上最好的东西. IOCP 是真正的异步 IO,意味着每次发起一个 IO 请求,该调用本身则立即返回, 而包括 IO ...
- DDD领域驱动设计:仓储
1 前置阅读 在阅读本文章之前,你可以先阅读: 什么是DDD DDD的实体.值对象.聚合根的基类和接口:设计与实现 2 什么是仓储? 仓储封装了基础设施来提供查询和持久化聚合操作. 它们集中提供常见的 ...