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 ...
随机推荐
- Glassfish在SpringMVC服务端接收请求时字符出现乱码的解决办法
环境描述 前端:jsp 后端:SpringMVC Controller 尽管jsp页面已设置了pageEncoding: <%@page contentType="text/html& ...
- [2013-08-01]window.open
经常上网的朋友可能会到过这样一些网站,一进入首页立刻会弹出一个窗口,或者按一个连接或按钮弹出,通常在这个窗口里会显示一些注意事项.版权信息.警告.欢迎光顾之类的话或者作者想要特别提示的信息.其实制作这 ...
- sqlserver添加主键
sqlServer中给表添加主键的sql: alter table market_media_medical_history alter column pk_id bigint not null; a ...
- Sql Server本地高版本备份数据备份至远程低版本数据库方法
想要将Sqlserver高版本备份的数据还原到低版本SqlServer2008R2上去,但是这在SqlServer中是没法直接还原数据库的,通过以下方法可以顺利还原. 通过高版本生成sql脚本在低版本 ...
- context:component-scan标签的use-default-filters属性的作用以及原理分析
一.背景 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,那么本文就来分析下出现这种问题可能的原因以及解决方式. 二.分析及原理窥探 1.项目结构 ...
- 用typedef定义函数指针的问题
在学习windows API的时候,遇到下面这段代码 以前见过的typedef的用法都是给一个数据类型取一个别名 typedef oldTypeName newTypeName 这种给数据类型 ...
- iOS 之各种Crash
1.*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position ...
- Linux远程服务器上安装SVN
前言 SVN服务器有2种运行方式:独立服务器和借助apache.2种方式各有利弊,独立SVN服务器不结合Apache安装使用,连接独立SVN服务器也不用HTTP协议.这是比较快捷的SVN服务器配置方法 ...
- MVC 解决 readonly 问题
<input type="text" class="form-control" name="UR_UserName" value=&q ...
- 【Java EE 学习 73】【数据采集系统第五天】【参与调查】【导航处理】【答案回显】【保存答案】
一.参与调查的流程 单击导航栏上的“参与调查”按钮->EntrySurveyAction做出相应,找到所有的Survey对象并转发到显示所有survey对象的页面上供用户选择->用户单击其 ...