Spring注解 - AOP 面向切面编程
基本概念:
AOP: Aspect Oriented Programming,即面向切面编程
指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式
- 前置通知(@Before):在目标方法运行之前执行
- 后置通知(@After):在目标方法运行结束之后执行(无论方法执行成功,还是出现异常,都执行)
- 返回通知(@AfterReturning):在目标方法正常返回之后执行
- 异常通知(@AfterThrowing):在目标方法出现异常后执行
- 环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
返回通知和异常通知只能同时执行一个
基本使用:
导入aop依赖:Spring AOP(spring-aspects)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
定义一个业务逻辑类
public class MathCalculator {
public int div(int a, int b){
return a / b;
}
}
定义一个日志切面类,告诉 Spring 这个类是切面类(在切面类加上:@Aspect),并且,给目标方法标注“何时何地”执行(通知注解)
切入点基本格式:权限修饰符 方法返回值 全方法名(参数列表类型) 异常类型,加粗的是必填项
在@Before注解上引用,如果引用类外的切入点表达式,需要使用全方法名
@Aspect
public class LogAspects { //抽取公共的切入点表达式
@Pointcut("execution(* com.spring.aop.MathCalculator.*(..))")
public void pointCut(){
} @Before("pointCut()")
public void logStart() {
System.out.println("方法运行");
} @After("pointCut()")
public void logEnd() {
System.out.println("方法结束");
} @AfterReturning("pointCut()")
public void logReturn() {
System.out.println("方法正常返回");
} @AfterThrowing("pointCut()")
public void logException() {
System.out.println("方法出现异常");
}
}
将切面类和业务逻辑类(目标方法所在类)都注册到ioc容器中,注册的几种方式可以参考前面的博客Spring注解 - 组件的注册
@Bean
public MathCalculator mathCalculator(){
return new MathCalculator();
} @Bean
public LogAspects logAspects(){
return new LogAspects();
}
开启基于注解的AOP模式(在配置类加上:@EnableAspectJAutoProxy)
@Configuration
@EnableAspectJAutoProxy
public class SpringConfiguration
测试一把
@Test
public void test1(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration6.class);
MathCalculator mathCalculator = (MathCalculator) applicationContext.getBean("mathCalculator");
mathCalculator.div(2, 1);
}
运行结果:
-----正常运行-----
方法运行
方法结束
方法正常返回
-------异常------
方法运行
方法结束
方法出现异常
但上述输出的内容太简单了,如果想要得到方法具体信息怎么办?我们对切面类做一些改造
@Aspect
public class LogAspects {
@Pointcut("execution(* com.spring.aop.MathCalculator.*(..))")
public void pointCut() {
}
@Before(value = "pointCut()")
public void logStart(JoinPoint joinPoint) {
//参数列表
Object[] args = joinPoint.getArgs();
//方法名
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName + "方法运行,参数列表:" + Arrays.toString(args));
}
@After("pointCut()")
public void logEnd(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName + "方法结束");
}
@AfterReturning(value = "pointCut()", returning = "returnInfo")
public void logReturn(JoinPoint joinPoint, Object returnInfo) {
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName + "方法正常返回,返回值:" + returnInfo);
}
@AfterThrowing(value = "pointCut()", throwing = "exception")
public void logException(JoinPoint joinPoint, Exception exception) {
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName + "方法出现异常,异常信息:" + exception);
}
}
运行结果:
-----正常运行-----
div方法运行,参数列表:[2, 1]
div方法结束
div方法正常返回,返回值:2
-------异常------
div方法运行,参数列表:[2, 0]
div方法结束
div方法出现异常,异常信息:java.lang.ArithmeticException: / by zero
注意:参数joinPoint一定要放在参数表的第一位
Spring注解 - AOP 面向切面编程的更多相关文章
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- Spring 08: AOP面向切面编程 + 手写AOP框架
核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- Spring框架——AOP面向切面编程
简介 AOP练习 使用动态代理解决问题 Spring AOP 用AspectJ注解声明切面 前置后置通知 利用方法签名编写AspectJ切入点表达式 指定切面的优先级 基于XML的配置声明切面 Spr ...
- Spring框架 AOP面向切面编程(转)
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- spring:AOP面向切面编程02
参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...
- Spring之AOP(面向切面编程)_入门Demo
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可 ...
- spring:AOP面向切面编程(注解)03
使用注解写aop时最好使用环绕通知写 切面类: /** * 用于记录日志的工具类,它里面提供了公共的代码 */ @Component("logger") @Aspect //表示当 ...
- 【spring源码学习】spring的AOP面向切面编程的实现解析
一:Advice(通知)(1)定义在连接点做什么,为切面增强提供织入接口.在spring aop中主要描述围绕方法调用而注入的切面行为.(2)spring定义了几个时刻织入增强行为的接口 => ...
随机推荐
- Qt QString与int的转换
QString转int QString a=" ; int b; b=a.toInt(); int 转 QString ; QString b; b=QString::number(a)
- 浏览器证书问题,chorm,ie,edge,safari都会去读系统证书,firefox例外
坑爹 没想过浏览器兼容的问题. 为系统安装用户证书后, firefox一直无法连接 提示 连接 www.httpsserver.com:8985 时发生错误. SSL 对等端无法协商出一个可接受的安全 ...
- python2下经典爬虫(第一卷)
python2.7的爬虫个人认为比较经典在此我将会用书中的网站http://example.webscraping.com作为案例 爬虫第一步:进行背景调研 了解网站的结构资源在网站的robots.t ...
- Java IO: FileInputStream
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) FileInputStream可以以字节流的形式读取文件内容.FileInputStream ...
- 吴裕雄--天生自然python学习笔记:Python3 命名空间和作用域
命名空间(Namespace)是从名称到对象的映射,大部分的命名空间都是通过 Python 字典来实现的. 命名空间提供了在项目中避免名字冲突的一种方法.各个命名空间是独立的,没有任何关系的,所以一个 ...
- MyBatis学习总结之一对多映射
1.首先创建2张表:students 和grades create table grades( gid ) primary key, gname varchar() ); create table s ...
- TICA 2019 自动的自动化测试——智能化一站式的API测试服务
阿里QA导读:新奥集团中台的陈磊为我们打开了AI测试驱动的视野,同时也深入浅出地介绍了如何打造智能化API测试框架.通过陈磊老师的分享,我们看到了AI-DT的无限可能性.以后,AI能不能代替大部分手动 ...
- [大餐]开发摘记1--我的Fragment通信的框架
[大餐]开发摘记1--我的Fragment通信的框架 | 卖牙膏的芖口钉 盒子 盒子 博客 分类 标签 友链 大专栏 [大餐]开发摘记1--我的Fragment通信的框架ass="ROUN ...
- 推荐一款在UBUNTU下使用的编辑器
偶然的机会 ,发现了这款软件,以前一直是在用gedit编辑器,但在WINDOWS下写的文档,在ubuntu下用gedit打开后,复制有换行的问题,一直没解决,所以在网上找到了这款软件,安装使用了几天, ...
- PHP创建缓存文件
文件操作类 <?php /** 文件操作类 */ class FileIO { /** * 读取目录 * @param string $dirPath dir名称 * @return array ...