SpringAOP 通知(advice)
@Aspect
@Order(1)
public class AopOne { /**
* 目标方法执行之前
* @param joinPoint
*/
@Before("execution(* com.gary.operation.*.*(..))")
public void befor(JoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
System.out.println("AopOne.befor()");
} /**
* 目标方法执行以后 无论是否出现异常 相当于finally语句块
* 该方法优先于@AfterReturning
* @param joinPoint
*/
@After("execution(* com.gary.operation.*.*(..))")
public void after(JoinPoint joinPoint) {
System.out.println("AopOne.after()");
} /**
* 方法返回以后 没有出现异常的情况
*/
@AfterReturning(pointcut="execution(* com.gary.operation.*.*(..))", returning="returnVal")
public void afterReturning(JoinPoint joinPoint,Student returnVal) {
System.out.println("AopOne.afterReturning()" + returnVal.getName());
} /**
* 方法抛出异常时执行
* @param joinPoint
*/
@AfterThrowing(pointcut="execution(* com.gary.operation.*.*(..))", throwing="throwable")
public void afterThrowing(JoinPoint joinPoint, Throwable throwable) {
System.out.println("AopOne.afterThrowing()" + throwable.getMessage());
} /**
* 环绕执行 可以自由控制目标方法
* @param proceedingJoinPoint
* @return
*/
//@Around("execution(* com.gary.operation.*.*(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) {
Object result = null;
try {
System.out.println("AopOne.around()-begin");
result = proceedingJoinPoint.proceed();
System.out.println("AopOne.around()-end");
} catch (Throwable e) {
e.printStackTrace();
}
return result;
} }
1、概念术语
在开始之前,需要理解Spring aop 的一些基本的概念术语(总结的个人理解,并非Spring官方定义):
切面(aspect):用来切插业务方法的类。
连接点(joinpoint):是切面类和业务类的连接点,其实就是封装了业务方法的一些基本属性,作为通知的参数来解析。
通知(advice):在切面类中,声明对业务方法做额外处理的方法。
切入点(pointcut):业务类中指定的方法,作为切面切入的点。其实就是指定某个方法作为切面切的地方。
目标对象(target object):被代理对象。
AOP代理(aop proxy):代理对象。
通知:
前置通知(before advice):在切入点之前执行。
后置通知(after advice): 相当于finally语句块,无论是否出现异常都执行
后置通知(after returning advice):在切入点执行完成后,执行通知。(出现异常不执行)
环绕通知(around advice):包围切入点,调用方法前后完成自定义行为。
异常通知(after throwing advice):在切入点抛出异常后,执行通知。
2、常用表达式总结
1、任何一个目标对象声明的类型有一个 @Transactional 注解的连接点
@Pointcut("@within(org.springframework.transaction.annotation.Transactional)")
2、任何一个执行的方法有一个 @Transactional 注解的连接点
@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")
3、任何一个只接受一个参数,并且运行时所传入的参数是java.lang.String接口的连接点
@Pointcut("args(java.lang.String)")
4、任何一个在名为'tradeService'的Spring bean之上的连接点
@Pointcut("bean(UserDao)")
任何一个在名字匹配通配符表达式'*Service'的Spring bean之上的连接点
@Pointcut("bean(*Dao)")
5、组合使用 && 并且的意思。 即俩个条件都需要满足。
@Before("aPointcut() && args(java.lang.String)")
6、组合使用并且接收参数
@Before("aPointcut() && args(s)")
public void beforeAdvice(String s) {
System.out.println("before advice is executed!" + s);
}
7、组合使用接收参数和注解
@Before("aPointcut() && args(s) && @annotation(tx)")
public void beforeAdvice(String s, Transactional tx) {
System.out.println("before advice is executed!" + tx);
}
8、组合使用接收注解
@Before(value="execution(* com.gary.operation.demo.proxy.*.*(..)) && @annotation(tx)")
public void beforeAdvice(Transactional tx) {
System.out.println("before advice is executed!" + tx);
}
SpringAOP 通知(advice)的更多相关文章
- 011-Spring aop 002-核心说明-切点PointCut、通知Advice、切面Advisor
一.概述 切点Pointcut,切点代表了一个关于目标函数的过滤规则,后续的通知是基于切点来跟目标函数关联起来的. 然后要围绕该切点定义一系列的通知Advice,如@Before.@After.@Af ...
- Spring笔记07(Spring AOP的通知advice和顾问advisor)
1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...
- 通知advice
基于注解的Spring AOP开发,来自https://www.cnblogs.com/junzi2099/p/8274813.html 1.定义目标类接口和实现类 2.编写Spring AOP的as ...
- 有哪些类型的通知Advice?
Before - 这些类型的 Advice 在 joinpoint 方法之前执行,并使用 @Before 注解标记进行配置. After Returning - 这些类型的 Advice 在连接点方法 ...
- 什么是通知Advice?
特定 JoinPoint 处的 Aspect 所采取的动作称为 Advice.Spring AOP 使用一个 Advice 作为拦截器,在 JoinPoint "周围"维护一系列的 ...
- Spring 通知(Advice)和顾问(Advisor)
AOP ( Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring中的通知(Advice)和顾问(Advisor)
在Spring中,目前我学习了几种增强的方式,和大家分享一下 之前的话: 1.AOP (Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect O ...
- Spring学习(十五)----- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
随机推荐
- .net完整的图文验证
摘自:http://blog.csdn.net/durongjian/article/details/4336380 一.创建ValidaeCode类库工程: 1.创建ValidaeCode类库工程, ...
- Android 系统 root 破解原理分析
现在Android系统的root破解基本上成为大家的必备技能!网上也有很多中一键破解的软件,使root破解越来越容易.但是你思考过root破解的 原理吗?root破解的本质是什么呢?难道是利用了Lin ...
- Linux命令-实时监测命令:watch
watch 是一个非常实用的命令,基本所有的 Linux 发行版都带有这个小工具,如同名字一样,watch 可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行..在Linux下,watch是周期性 ...
- 修改jQuery.validate验证方法和提示信息
1.添加验证方法 在jquery.validate.js文件中直接添加验证方法,例如: jQuery.validator.addMethod("Specialstring", fu ...
- 【ERROR】EXP-00091
问题: 在我们做exp的过程中可能经常会遇到EXP-00091: Exporting questionable statistics.这样的EXP信息,其实它就是exp的error message,它 ...
- Android仿联系人列表分组悬浮列表实现,自己定义PinnedHeaderListView实现
效果 (关于gif怎么生成的.我先录手机的屏幕得到mp4文件.然后用这个网址:https://cloudconvert.com/mp4-to-gif 进行的mp4转换gif,使用的时候须要又一次选择g ...
- [转]Ubuntu上安装TL-WN725N 2.0无线网卡驱动
笔者使用的Ubuntu操作系统是13.04版本的,这个版本下,笔者使用朋友的TL-WN725N 1.0版本的无线网卡,直接插上就可以使用.正是由于这个方便性,不用去折腾什么驱动,所以笔者从京东上买了一 ...
- 用jQuery.ajaxWebService请求WebMethod,Ajax处理实现局部刷新;及Jquery传参数,并跳转页面 用post传过长参数
首先在aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性. 如: [WebMethod] public static string GetUserName() { //. ...
- mysql-5.7.20 版本的 mysql-group-replication 可用性测试报告
一.喜迎 mysql-5.7.20 事实上mysql-group-replication 功能是在mysql-5.7.17这个版本上引入的,它实现了mysql各个结点间数据强一致性, 这个也成为了我 ...
- CSS选择器的优化
前面花了几个篇幅着重介绍了CSS的选择器的使用,我将其分成三个部分:CSS基本选择器.CSS属性选择器以及CSS伪类选择器.那么今天我主要想和大家一起来学习——CSS选择器方面的性能优化.因为对性能这 ...