Spring AOP前置通知和后置通知
Spring AOP
AspectJ:Java社区里最完整最流行的AOP框架
在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP
在Spring中启用AspectJ注解支持
要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar、aspectj.weaver.jar和spring-aspects.jar
将aop Schema添加到<beans>根元素中。
要在Spring IOC容器中启用AspectJ注解支持,只要早bean配置文件中定义一个空的XML元素<aop:aspectj-autoproxy>
当Spring IOC容器侦测到bean配置文件中的<aop:aspectj-autoproxy>元素时,会自动为与AspectJ切面匹配的bean创建代理
用AspectJ注解声明切面
要在Spring中声明AspectJ切面,只需要在IOC容器中将切面声明为bean实例。当在Spring IOC容器中初始化AspectJ切面之后,Spring IOC容器就会为那些与AspectJ切面相匹配的bean创建代理
在AspectJ注解中,切面只是一个带有@AspectJ注解的Java类
通知是标注有某种注解的简单的Java方法
AspectJ支持5种类型的通知注解:
@Before:前置通知,在方法执行之前返回
@After:后置通知,在方法执行后执行
@AfterRunning:返回通知,在方法返回结果之后执行
@AfterThrowing:异常通知,在方法抛出异常之后
@Around:环绕通知,围绕着方法执行
利用方法签名编写AspectJ切入点表达式
最典型的切入点表达式时根据方法的签名来匹配各种方法:
-execution * com.yl.spring.aop.ArithmeticCalculator.*(..):匹配ArithmeticCalculator中声明的所有方法,第一个*代表任意修饰符及任意返回值,第二个*代表任意方法,..匹配任意数量的参数。若目标类与接口与切面在同一个包中,可以省略包名。
-execution public * ArithmeticCalculator.*(..):匹配ArithmeticCalculator接口的所有公有方法
-execution public double ArithmeticCalculator.*(..):匹配ArithmeticCalculator中返回double类型数值的方法
-execution public double ArithmeticCalculator.*(double, ..):匹配第一个参数为double类型的方法,..匹配任意数量任意类型的参数
-execution public double ArithmeticCalculator.*(double, double):匹配参数类型为double,double类型的方法
后置通知
后置通知是在连接点完成之后执行的,即连接点返回结果或者抛出异常的时候,下面的后置通知记录了方法的终止。
一个切面可以包括一个或者多个通知
LoggingAspect.java
1 package com.yl.spring.aop.impl; 2 3 import java.util.Arrays; 4 import java.util.List; 5 6 import org.aspectj.lang.JoinPoint; 7 import org.aspectj.lang.annotation.After; 8 import org.aspectj.lang.annotation.Aspect; 9 import org.aspectj.lang.annotation.Before; 10 import org.springframework.stereotype.Component; 11 12 //这个类声明为一个切面:需要把该类放入到IOC容器中;再声明为一个切面 13 @Aspect 14 @Component 15 public class LoggingAspect { 16 17 //声明该方法是一个前置通知:在目标方法开始之前执行 18 //@Before("execution(public int com.yl.spring.aop.impl.ArithmeticCalculatorImpl.add(int, int))") 19 @Before("execution(* com.yl.spring.aop.impl.*.*(..))") 20 public void beforeMethod(JoinPoint joinpoint) { 21 String methodName = joinpoint.getSignature().getName(); 22 List<Object> args = Arrays.asList(joinpoint.getArgs()); 23 System.out.println("The method " + methodName + " begins with " + args); 24 } 25 //后置通知:在目标方法执行后(无论是否发生异常),执行的通知 26 //在后置通知中,还不能访问目标方法执行的结果 27 @After("execution(* com.yl.spring.aop.impl.*.*(..))") 28 public void afterMethod(JoinPoint joinPoint) { 29 String methodName = joinPoint.getSignature().getName(); 30 List<Object> args = Arrays.asList(joinPoint.getArgs()); 31 System.out.println("The method " + methodName + " end with " + args); 32 } 33 34 }
配置文件applicationContext.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 9 10 <!-- 配置自动扫描的包 --> 11 <context:component-scan base-package="com.yl.spring.aop.impl"></context:component-scan> 12 13 <!-- 使AspectJ注解起作用:自动为匹配的类生成代理对象 --> 14 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 15 16 </beans>
测试类:
1 package com.yl.spring.aop.impl; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class Main { 7 public static void main(String[] args) { 8 9 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 10 11 ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); 12 13 int result = arithmeticCalculator.add(4, 6); 14 System.out.println("result: " + result); 15 16 result = arithmeticCalculator.mul(4, 6); 17 System.out.println("result: " + result); 18 } 19 }
转载于:https://www.cnblogs.com/dreamfree/p/4095858.html
Spring AOP前置通知和后置通知相关推荐
- Spring AOP中的前置通知和后置通知详解
不同版本的spring对AOP的支持有所不同,spring2.0之前,它主要针对不同类型的拦截器使用XML配置文件通过代理来实现.而spring2.0之后,它可以使用JDK5的注解来完成AOP的实现, ...
- spring之aop(前置通知,后置通知,环绕通知,过滤通知,异常通知)
1.AOP中关键性概念 连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出 目标(Target):被通知(被代理)的对象 注1:完成具体的业务逻辑 通知(Advice ...
- spring之AOP(面向切面编程)和五大通知(前置通知、后置通知、异常通知、环绕通知、过滤通知)
一.aop的介绍 1.AOP中关键性概念 : 连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出. 目标(Target):被通知(被代理)的对象 注1:完成具体的业务逻 ...
- Spring Boot AOP面向切面编程使用(定义切入点、前置通知、后置通知、返回通知、异常通知、环绕通知)
1 AOP AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发 ...
- 配置切入点表达式|| 前置通知、后置通知、异常通知、最终通知、环绕通知
环绕通知 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- Spring3.0中的前置通知、后置通知、环绕通知、异常通知
观众类Audience~~ [java] view plain copy package com.jCuckoo.demo; import org.aspectj.lang.ProceedingJoi ...
- Spring AOP的—绍和5种通知类型的使用(详解)
Spring AOP介绍与使用 文章目录 Spring AOP介绍与使用 1.AOP的概念 AOP的核心概念及术语 AOP的通知类型(5种) AOP的应用场景 2.Spring AOP的配置(基于Ma ...
- spring21:Aspectj实现后置通知@AfterReturning
切面类: package com.atChina.Test2;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation ...
- C++中前置操作符和后置操作符的重载
文章目录 1 C++中前置操作符和后置操作符的重载 1.1 前置操作符和后置操作符的原生语义 1.2 ++操作符的重载 1.3 前置++和后置++的真正区别 1 C++中前置操作符和后置操作符的重载 ...
- HTML5调用手机前置摄像头或后置摄像头拍照,canvas显示,经过Android测试
为什么80%的码农都做不了架构师?>>> 但是navigator.getUserMediau已经从 Web 标准中删除,虽然部分浏览器可以使用,生产环境中还是要做好兼容.新的A ...
最新文章
- 【转】memcached工作原理介绍
- 什么是COM[网摘]
- 以太坊知识教程------交易
- 人工智能 | 自动驾驶与人工智能前沿研究报告(技术篇)
- 设置gbk_我的gVim设置
- 【Python】疫情卷土重来?Python可视化带你追踪疫情的最新动态
- Filter_细节_过滤器链(多个过滤器)
- Kali忘记登录密码——修改root密码
- thinkphp中表有前缀名的时候申明模板的方法
- 低功耗广域网:关键特性
- 2021牛客暑期多校训练营9,签到题HE
- springboot16 整合MyBatis
- 计算机基础excel操作试题,大学计算机基础 excel测试题 求答案~~喵~~
- Python网站服务器搭建,python 最快速搭建一个网站
- 用root登录亚马逊云
- opencv无法打开摄像头
- 物体重心的特点是什么_重心是什么的交点?
- 计算机外设配件的主要相关参数有哪些,电脑配件有哪些
- 子曾经曰过,一晃如隔世,三周荒废逝。
- SegNet——论文笔记