Spring的通知(Advice)
Spring提供了5种Advice类型:
Interception Around:JointPoint前后调用
Before:JointPoint前调用
After Returning:JointPoint后调用
Throw:JoinPoint抛出异常时调用
Introduction:JointPoint调用完毕后调用
Interception Around通知
Interception Around通知会在JoinPoint的前后执行。实现此通知的类需要实现接口MethodInterceptor,示例代码如下:
public class LogInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation invocation invocation) throws Throwable{
System.out.println("开始审核数据...");
Object rval = invocation.proceed();
System.out.println("审核数据结束...");
return rval;
}
}
Before通知
只在JointPoint前执行,实现Before通知的类需要实现接口MethodBeforeAdvice,示例带入如下:
1 public class LogBeforeAdvice implements MethodBeforeAdvice{
2 public void before(Method m,Object[] args,Object target) throw Throwable{
3 System.out.println("开始审核数据...");
4 }
5 }
After Returning通知
只在JointPoint之后执行,实现After Returning通知的类需要实现接口AfterReturningAdvice,示例代码如下:
public class LogAfterAdvice implements AfterReturningAdvice{
public void afterReturning(Method m,Object[] args,Object target) throws Throwable{
System.out.println(“审核数据结束...”);
}
}
Throw通知
只在JointPoint抛出异常时执行,实现Throw通知的类需要实现接口ThrowsAdvice,示例代码如下:
public class LogThrowAdvice implements ThrowsAdvice{
public void afterThrowing(RemoteException ex) throws Throwable{
System.out.println("审核数据异常,请检查..."+ex);
}
}
Introduction通知
只在JointPoint调用完毕后执行,实现Introduction通知的类需要实现接口IntroductionAdvisor和接口IntroductionInterceptor
Spring的通知(Advice)的更多相关文章
- Spring学习(十五)----- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-通知(Advice)API
本文翻译自Spring.NET官方文档Version 1.3.2. 受限于个人知识水平,有些地方翻译可能不准确,但是我还是希望我的这些微薄的努力能为他人提供帮助. 侵删. 让我们看看 Spring.N ...
- Spring笔记07(Spring AOP的通知advice和顾问advisor)
1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...
- SpringAOP 通知(advice)
@Aspect @Order(1) public class AopOne { /** * 目标方法执行之前 * @param joinPoint */ @Before("executi ...
- Spring系列26:Spring AOP 通知与顺序详解
本文内容 如何声明通知 如何传递参数到通知方法中 多种通知多个切面的通知顺序 多个切面通知的顺序源码分析与图解 声明通知 Spring中有5种通知,通过对应的注解来声明: @BeforeBefore ...
- spring事件通知机制详解
优势 解耦 对同一种事件有多种处理方式 不干扰主线(main line) 起源 要讲spring的事件通知机制,就要先了解一下spring中的这些接口和抽象类: ApplicationEventPub ...
- Spring的通知类型,切入表达式写法
转载自 https://www.cnblogs.com/ltfxy/p/9882697.html Spring中通知类型: 前置通知:目标方法执行之前进行操作,可以获得切入点信息 后置通知: 目标方 ...
- Spring横切面(advice),增强(advisor),切入点(PointCut)(转)
Spring横切面(advice),增强(advisor),切入点(PointCut)的一点理解: 1.Spring管理事务有2种,其中一种是HibernateTransactionManager管理 ...
- 011-Spring aop 002-核心说明-切点PointCut、通知Advice、切面Advisor
一.概述 切点Pointcut,切点代表了一个关于目标函数的过滤规则,后续的通知是基于切点来跟目标函数关联起来的. 然后要围绕该切点定义一系列的通知Advice,如@Before.@After.@Af ...
随机推荐
- sh 测试网段在线主机
yum install nmap nmap -sP 192.168.21.1/24 查看网段在线主机 grep -vwf file1 file2 文件内容比较 #!/bin/bash # day=` ...
- 中国知网cnki(永久会员账号)
中国知网cnki(永久会员账号)大男孩免费分享 网站简介: (中国知网http://www.cnki.net/)中国知网是国家知识基础设施(National Knowledge Infrastru ...
- Mysql InnoDB 共享表空间和独立表空间
前言:学习mysql的时候总是习惯性的和oracle数据库进行比较.在学习mysql InnoDB的存储结构的时候也免不了跟oracle进行比较.Oracle的数据存储有表空间.段.区.块.数据文件: ...
- 文件上传之 HttpPostedFile
HttpPostedFile类,提供对客户端已上载的单独文件的访问. 公共属性如下: SaveAs()方法,用于保存上传文件的内容. 用法为: #region 文件上传 /// <summary ...
- 【HTML5】 web上的音频
<!-- audio通过属性的设置可以控制音频播放的行为: 表6-2 audio元素的属性 ——————————————————————————————————————————————————— ...
- 1Z0-050
QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- [Idea] idea打不开项目,原因很莫名
由于项目是gitlab上存储的,所以下下来之后,之前遇到过,以为是重新下载之后master上面没有内容导致无法正常打开,这种情况,切换一下master再打开即可: 但是这次遇到的问题不是这种情况, 使 ...
- Log4Net记录日志的使用
Log4net 基本样式: <log4net> <appender name="LogFileAppender" type="log4net.Appen ...
- [翻译]Telnet简单介绍及在windows 7中开启Telnet客户端
文章翻译自 http://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-clien ...