spring aop 的五种通知类型
本文转自:http://blog.csdn.net/cqabl/article/details/46965197
spring aop通知(advice)分成五类:
前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常。
正常返回通知[After returning advice]:在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行。
异常返回通知[After throwing advice]:在连接点抛出异常后执行。
返回通知[After (finally) advice]:在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容。
环绕通知[Around advice]:环绕通知围绕在连接点前后,比如一个方法调用的前后。这是最强大的通知类型,能在方法调用前后自定义一些操作。环绕通知还需要负责决定是继续处理join point(调用ProceedingJoinPoint的proceed方法)还是中断执行。
接下来通过编写示例程序来测试一下五种通知类型:
- 定义接口
package com.chenqa.springaop.example.service;
public interface BankService {
/**
* 模拟的银行转账
* @param from 出账人
* @param to 入账人
* @param account 转账金额
* @return
*/
public boolean transfer(String form, String to, double account);
}
- 编写实现类
package com.chenqa.springaop.example.service.impl;
import com.chenqa.springaop.example.service.BankService;
public class BCMBankServiceImpl implements BankService {
public boolean transfer(String form, String to, double account) {
if(account<100) {
throw new IllegalArgumentException("最低转账金额不能低于100元");
}
System.out.println(form+"向"+to+"交行账户转账"+account+"元");
return false;
}
}
- 修改spring配置文件,添加以下内容:
<!-- bankService bean -->
<bean id="bankService" class="com.chenqa.springaop.example.service.impl.BCMBankServiceImpl"/>
<!-- 切面 -->
<bean id="myAspect" class="com.chenqa.springaop.example.aspect.MyAspect"/>
<!-- aop配置 -->
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut expression="execution(* com.chenqa.springaop.example.service.impl.*.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
- 编写测试程序
ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
BankService bankService = context.getBean("bankService", BankService.class);
bankService.transfer("张三", "李四", 200);
执行后输出:
将测试程序中的200改成50,再执行后输出:
通过测试结果可以看出,五种通知的执行顺序为: 前置通知→环绕通知→正常返回通知/异常返回通知→返回通知,可以多次执行来查看。
spring aop 的五种通知类型的更多相关文章
- spring aop的五种通知类型
昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. ...
- Java开发学习(十六)----AOP切入点表达式及五种通知类型解析
一.AOP切入点表达式 对于AOP中切入点表达式,总共有三个大的方面,分别是语法格式.通配符和书写技巧. 1.1 语法格式 首先我们先要明确两个概念: 切入点:要进行增强的方法 切入点表达式:要进行增 ...
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...
- SpringAOP使用注解实现5种通知类型
spring aop的5种通知类型都有 Before前置通知 AfterReturning后置通知 Around环绕通知 AfterThrowing异常通知 After最终通知 首先创建接口和实现类 ...
- 学习ActiveMQ(五):activemq的五种消息类型和三种监听器类型
一.前面我们一直发送的是字符串类型,其实activemq一共支持五种消息类型: 1.String消息类型:发送者:消费者: 1.String消息类型:发送者:消费者: 1.String消息类型:发送者 ...
- 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!
转载自:https://github.com/whvcse/RedisUtil 代码 ProtoStuffSerializerUtil.java import java.io.ByteArrayInp ...
- spring AOP的两种代理
本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理 2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...
- Lua的五种变量类型、局部变量、全局变量、lua运算符、流程控制if语句_学习笔记02
Lua的五种变量类型.局部变量.全局变量 .lua运算符 .流程控制if语句 Lua代码的注释方式: --当行注释 --[[ 多行注释 ]]-- Lua的5种变量类型: 1.null 表示 ...
- spring boot的几种配置类型
1.spring boot的几种配置类型 1)基本配置,spring自动读取的,全都在application.yml里配置,spring会自动读取这个配置文件 2)个性化配置:比如配置intercep ...
随机推荐
- 测开之路七十四:python处理kafka
kafka-python地址:https://github.com/dpkp/kafka-python 安装kafka-python:pip install kafka-python 接收消息 fro ...
- git查看某个文件的提交历史
1. git log --pretty=oneline 文件名 文件名是文件路径+文件名,输入完整 输入正确后,打印出版本号的列表 2. git show <git提交版本号> <文 ...
- 使用 GitLab 的 OAuth2 认证服务
原文地址 本文档讲述如何使用 GitLab 作为 OAuth 认证服务提供商,以通过 GitLab 的 OAuth 认证登录其他服务(例如持续集成工具 Drone). 如果想使用其他 OAuth 身份 ...
- JSP+JavaBean 登陆验证
1.java package cn.gs.ly; import java.util.HashMap; import java.util.Map; public class Register { pri ...
- Vue过渡:用Velocity实现JavaScript钩子
Velocity is an animation engine with a similar API to jQuery's $.animate(). It has no dependencies, ...
- jdk (Java Development Kit)
JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具. JD ...
- JavaScript搜索框响应事件
HTML页面,注意:不要使用form标签 <input type = "text" name="keyword" id="keyword&quo ...
- 20190825 On Java8 第十三章 函数式编程
第十三章 函数式编程 函数式编程语言操纵代码片段就像操作数据一样容易. 虽然 Java 不是函数式语言,但 Java 8 Lambda 表达式和方法引用 (Method References) 允许你 ...
- TypeError: 'generator' object is not subscriptable
TypeError: 'generator' object is not subscriptable 生成器对象不可以带下标 def get_row(self,row_no): if not isin ...
- Java 遍历某个目录
import java.io.File; import java.io.IOException; public class DirErgodic { public static void find(S ...