Spring AOP面向切面编程 通知类型
Spring AOP面向切面编程 通知类型
通知分为:
- 前置通知
执行方法之前通知
- 后置通知
执行方法之后通知
- 异常通知
相当于cache里面的内容
- 最终通知
相当于finally
- 环绕通知
前四种通知集合
代码实现
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zjw</groupId>
<artifactId>day03_eesy_04adviceType</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<encoding>UTF-8</encoding>
<spring.version>6.1.1</spring.version>
<aspectjweaver.version>1.9.21</aspectjweaver.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
</project>
Spring配置文件
<?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">
<!--配置spring的Ioc,把service对象配置进来-->
<bean id="accountService" class="com.zjw.service.impl.AccountServiceImpl" />
<!--配置Logger类-->
<bean id="logger" class="com.zjw.utils.Logger" />
<!--配置AOP-->
<aop:config>
<!--切入点-->
<aop:pointcut id="pt1" expression="execution(* com.zjw.service.impl.*.*(..))"/>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--前置通知 :执行方法之前通知-->
<!--<aop:before method="beforePrintLog" pointcut-ref="pt1"/>-->
<!--后置通知 :执行方法之后通知-->
<!--<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"/>-->
<!--异常通知 :相当于cache里面的内容-->
<!--<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"/>-->
<!--最终通知 :相当于finally-->
<!--<aop:after method="afterPrintLog" pointcut-ref="pt1"/>-->
<!--环绕通知-->
<aop:around method="aroundPrintLog" pointcut-ref="pt1"/>
<!--配置切入点表达式 id属性用于指定表达式的唯一表标识。expression属性用于指定表达式的内容
此标签写在aop:aspect标签内部只能当前切面使用。
它还可以写在aop:aspect标签外面,此时就变成了所有切面可用
-->
<!--<aop:pointcut id="pt1" expression="execution(* com.zjw.service.impl.*.*(..))"/>-->
</aop:aspect>
</aop:config>
</beans>
Service层
package com.zjw.service.impl;
import com.zjw.service.IAccountService;
/**
* @author zjw
*/
public class AccountServiceImpl implements IAccountService {
@Override
public void saveAccount() {
System.out.println("saveAccount().....");
int i = 1/0;
}
@Override
public void updateAccount(int i) {
System.out.println("updateAccount(int i)...."+i);
}
@Override
public int deleteAccount() {
System.out.println("deleteAccount()......");
return 0;
}
}
通知类
package com.zjw.utils;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* 用于记录日志的工具类,它里面提供了公共的代码
* @author zjw
*/
public class Logger {
/**
* 前置通知
*/
public void beforePrintLog(){
System.out.println("前置通知..........执行方法之前");
}
/**
* 后置通知
*/
public void afterReturningPrintLog(){
System.out.println("后置通知..........执行方法结束");
}
/**
* 异常通知
*/
public void afterThrowingPrintLog(){
System.out.println("异常通知..........相当于cache里面的内容");
}
/**
* 最终通知
*/
public void afterPrintLog(){
System.out.println("最终通知..........相当于finally");
}
/**
* 环绕通知
* 问题:
* 当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了
* 分析:
* 通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有。
* 解决:
* Spring框架为我们提供了一个接口,ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。
* 该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
*
*/
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
System.out.println("环绕通知 .........前置通知");
//得到方法执行所需的参数
Object[] args = pjp.getArgs();
rtValue = pjp.proceed(args);
System.out.println("环绕通知 ..........后置通知");
return rtValue;
}catch (Throwable t){
System.out.println("环绕通知 ..........异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("环绕通知 ..........最终通知");
}
}
}
测试
package com.zjw.test;
import com.zjw.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试AOP的配置
*/
public class AOPTest {
public static void main(String[] args) {
//1、获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、获取对象
IAccountService accountService = (IAccountService)ac.getBean("accountService");
//3、执行方法
accountService.saveAccount();
}
}
结果
环绕通知 .........前置通知
saveAccount().....
环绕通知 ..........异常通知
环绕通知 ..........最终通知
Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
at com.zjw.utils.Logger.aroundPrintLog(Logger.java:58)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
...
...
Caused by: java.lang.ArithmeticException: / by zero
at com.zjw.service.impl.AccountServiceImpl.saveAccount(AccountServiceImpl.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
...
... 13 more
Spring AOP面向切面编程 通知类型的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【spring-boot】spring aop 面向切面编程初接触--切点表达式
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- 【spring-boot】spring aop 面向切面编程初接触
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- Spring AOP 面向切面编程入门
什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
随机推荐
- kubesphere应用系列(四)--创建自动流水线
第一步创建多分支流水线 复制生成的url,也可以在编辑设置时复制 第二步新增Jenkinsfile文件 新增Jenkinsfile文件放在根目录 方式一:官方示例:https://github.c ...
- 天线的OTA测试
有源测试 (即OTA测试) 把使用综测仪的测试叫做有源测试(Active).使用有源测试的测试速度比较无源相对要慢,但是因为手机是一个复杂材料体,往往无源测试对发射性能的模拟是可信的,但是对于接收性能 ...
- ABP登录返回错误次数、锁定时间
ABP默认登录返回错误结果时,不会显示错误次数.锁定时间.为了实现验证错误时返回错误次数.锁定时间,我们需要改造返回接口. 1.定位验证错误的地方: 修改部分代码 1 /// <summar ...
- 群晖NAS 6.2x Moments AI场景识别 补丁教程
首先,我们需要在套件中心里,停用Moments: 正常情况下,群晖是不允许root账号直接登录的,必须使用管理员账户,然后sudo -i指令登录root账户,这样非常的麻烦.所以我们可以之际开启roo ...
- SpringBoot - [02] 第一个SpringBoot程序
jdk maven3.6.3 springboot最新版 idea 如果使用官网 Spring Initializr ,则需要jdk17.21.22,并且是Springboot3.x 可以在idea创 ...
- Hi3516EV200 编译环境配置及交叉编译软件包
基础信息 OS: Ubuntu 16.04 xenial SDK 版本: Hi3516EV200R001C01SPC012 - Hi3516EV200_SDK_V1.0.1.1 SDK 包路径:Hi3 ...
- wangeditor编辑器
官网 https://www.wangeditor.com/ 在线体验DEMO https://codepen.io/xiaokyo-the-bold/pen/ZEpWByR
- ChatBI≠NL2SQL:关于问数,聊聊我踩过的坑和一点感悟
"如果说数据是新时代的石油,智能问数就是能让普通人也能操作的智能钻井平台." 这里是**AI粉嫩特攻队!** ,这段时间真的太忙了,不过放心,关于从零打造AI工具的coze实操下篇 ...
- AI 智能体引爆开源社区「GitHub 热点速览」
最近很火的 Manus 智能体是一款将你的想法转化为行动的工具,能够处理生活中的各种任务.一经发布便迅速走红,并间接引爆了开源社区. 这也导致上榜的全是 AI 智能体开源项目,比如无需邀请码的开源版 ...
- svn提示Node remains in conflict的解决办法
svn 更新提示Node remains in conflict 这个时候不管svn up多少次,都无法更新到最新的内容 解决办法: svn revert --depth=infinity * 其中* ...