[原创]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 ...
随机推荐
- NOIP2009潜伏者【B003】
[B003]潜伏者[难度B]—————————————————————————————————————————————————————————————————— [题目要求] R国和S国正陷入战火之中 ...
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest
A. Altitude 从小到大加入每个数,用set查找前驱和后继即可. 时间复杂度$O(n\log n)$. #include <bits/stdc++.h> using namespa ...
- Struts2 整合Spring(Maven,注解版)
这两天正在试验Struts2与Spring框架的整合,和他们各自的“注解”.今天就总结一下这两个框架怎么用注解进行整合. 一,加入两者的依赖包,除了两者的必要依赖外,还需要导入struts2-spri ...
- MySQL字段数据类型表
* MySQL支持所有标准SQL数值数据类型. 数值类型BIT数据类型保存位字段值,并且支持MyISAM.MEMORY.InnoDB和BDB表.作为SQL标准的扩展,MySQL也支持整数类型TINYI ...
- CSS:在IE浏览器下,元素下沉一行的解决办法
HTML: <ul> <li><a href="">嘻嘻嘻嘻嘻嘻</a><span>2015-12-17</spa ...
- websocket 实现聊天功能
<html> <head> <base href="<%=basePath%>"> <title>webscoket t ...
- hibernate 异常:Unexpected Exception caught setting
异常信息:Unexpected Exception caught setting 'outHeight' on 'class com.srpm.core.project.seismicFortific ...
- oracle优化
sql语句 优化方式: 1select 语句中避免用"*" oracle在解析时,会将“*”依次转换成所有的列名(数据字典完成)耗时 2尽量多用commit语句 及时提交,释放资源 ...
- 设计模式之六大原则——接口隔离原则(ISP)
设计模式之六大原则——接口隔离原则(ISP) 转载于:http://www.cnblogs.com/muzongyan/archive/2010/08/04/1792528.html 接口隔离原则 ...
- php变量和数组大小限制
前言:shopnc在默认拼接sql的时候会带上limit 1000 那么问题就来了,如果在使用系统的封装的方法,但是如果你没有带上->limit(false)就完蛋了 那么问题来了,在判断时候, ...