[原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.通知分类:
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行
@AfterRunning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
@Around: 环绕通知, 围绕着方法执行
关于方法签名 看第五点
2.前置通知

3.后置通知:在后置通知中,不能访问目标方法执行的结果

4.返回通知


5.异常通知

6.环绕通知


package com.jason.spring.aop.impl; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面
//1.需要将该类放入到IOC 容器中
@Component
//2.再声明为一个切面
@Aspect
public class LoggingAspect { //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法
//作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法
//支持通配符
//@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
@Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins " + args);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: afterMethod
* @Time:2016年12月1日
* @Description: 在方法执行后执行的代码,无论该方法是否出现异常
*
* @param joinPoint
*/
@After("execution(* com.jason.spring.aop.impl.*.*(int, int))")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " end " + args);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: afterReturning
* @Time:2016年12月1日
* @Description: 在方法正常结束后执行代码,放回通知是可以访问到方法的返回值
*
* @param joinPoint
*/
@AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")
public void afterReturning(JoinPoint joinPoint ,Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " end with " + result);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: afterThrowting
* @Time:2016年12月1日
* @Description: 在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码
*
* @param joinPoint
* @param ex
*/
@AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")
public void afterThrowting(JoinPoint joinPoint, Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs exceptions " + ex);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: around
* @Time:2016年12月1日
* @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数
* 环绕通知 类似于 动态代理的全过程
* ProceedingJoinPoint:可以决定是否执行目标方法
* 环绕通知必须有返回值,返回值即为目标方法的返回值
*
* @param proceedingJoinPoint
*/
@Around("execution(* com.jason.spring.aop.impl.*.*(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint){ Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName(); //执行目标方法
try {
//前置通知
System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs())); result = proceedingJoinPoint.proceed(); //后置通知
System.out.println("The method " + methodName + "end with" + result); } catch (Throwable e) {
//异常通知
System.out.println("The method occurs exception : " + e);
throw new RuntimeException();
}
//后置通知 System.out.println("The method " + methodName + "end with" + result); return result; } }
[原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知的更多相关文章
- Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)
AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记95:Hibernate 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring实战第八章学习笔记————使用Spring Web Flow
Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理
通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...
- Spring 源码学习笔记10——Spring AOP
Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...
随机推荐
- Salt安装(yum不可用时)
salt-master安装 [salt-master]# yum install salt-master 或者 curl -L http://bootstrap.saltstack.o ...
- 用于异步的BackgroundWorker
XAML代码: <Window x:Class="backgroundtest.MainWindow" xmlns="http://schemas.microsof ...
- Windows OS上安装运行Apache Kafka教程
Windows OS上安装运行Apache Kafka教程 下面是分步指南,教你如何在Windows OS上安装运行Apache Zookeeper和Apache Kafka. 简介 本文讲述了如何在 ...
- ArrayList 实现删除重复元素(元素为对象类型)
package 集合; import java.util.ArrayList;import java.util.Iterator; /* * 删除集合中的重复的元素(元素是对象形式的) * * Li ...
- Python实战:下载鬼灵报告有声小说
在家无聊,想看看小说,不过看的眼睛痛,就想着下个有声小说来听听.但风上找到的都是要一集一集下,还得重命名,122集啊,点到什么时候. 写个批处理下载的脚本.记录下过程. 一.老套路了,找到下载URL. ...
- ZK textbox Constraint验证
test.zul: <?page title="" contentType="text/html;charset=UTF-8"?> <zk x ...
- C++实现vector
用了双向链表,快排,<<,=,[]重载,还有erase的实现比较好玩 //my Vecter ;T need "operator<" #include <i ...
- linux引导流程
本章重点: 1.linux引导流程 2.linux运行级别 3.linux启动服务管理 4.GRUB配置与应用 5.启动故障分析解决 linux启动流程 1.固件(fireware):固话在硬件上的程 ...
- spring的显示装配bean(1)------通过XML文件装配
1:spring环境的简单搭建 (1)导入spring相关的jar包. 2:准备要进行装配的Java类 这里给出两个举例类 (1) (2) 3:配置XML文件 (1)在配置文件的顶部声明多个XML模式 ...
- 如何用shared_ptr减少锁的争用
在并发环境下锁的使用是家常便饭, 如何减少锁的使用是优化程序性能的一个方面. c++11里面新增了智能指针std::shared_ptr, 这个东西也许能给我们带来些启发. shared_ptr的一个 ...