Spring源码:Advice接口
文章目录
- 1.Advice接口使用
- 2.Advice接口源码分析
- 2.2 AfterReturningAdvice接口
- 2.3 ThrowsAdvice接口
- 2.4 MethodInterceptor接口
1.Advice接口使用
Advice是对附加方法(被代理的方法前后需要执行的)的描述。
在Spring AOP中支持4中类型的通知:
1:before advice 在方法执行前执行。
2:after returning advice 在方法执行后返回一个结果后执行。
3:after throwing advice 在方法执行过程中抛出异常的时候执行。
4:Around advice 在方法执行前后和抛出异常时执行,相当于综合了以上三种通知
student类
public class Student {private String name;private Integer age;public Student() {}//getter and setter......public void print() {System.out.println(this.name + "---" + this.age);}
}
通知
public class SpringBeforeAdviceMethod implements MethodBeforeAdvice {public void before(Method method, Object[] args, Object target) throws Throwable {System.out.println("SpringBeforeAdviceMethod before......");}
}public class SpringAfterAdviceMethod implements AfterReturningAdvice {public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("SpringAfterAdviceMethod after ......");}
}
配置xml文件
<context:component-scan base-package="com.spring.aop"/><bean id="springBeforeAdviceMethod" class="com.spring.aop.advice.SpringBeforeAdviceMethod"/><bean id="springAfterAdviceMethod" class="com.spring.aop.advice.SpringAfterAdviceMethod"/><bean id="student" class="com.spring.aop.advice.Student"><property name="name" value="zhuqiuhui"/><property name="age" value="19"/></bean><!--定义代理类--><bean id="studentProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="student"/><property name="interceptorNames"><list><value>springBeforeAdviceMethod</value><value>springAfterAdviceMethod</value></list></property></bean>
main方法
public class AopMain {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) applicationContext.getBean("studentProxy");student.print();}
}
2.Advice接口源码分析
package org.aopalliance.aop;
public interface Advice {}
##2.1 MethodBeforeAdvice接口
public interface BeforeAdvice extends Advice {}public interface MethodBeforeAdvice extends BeforeAdvice {void before(Method method, Object[] args, Object target) throws Throwable;
}
2.2 AfterReturningAdvice接口
public interface AfterAdvice extends Advice {}public interface AfterReturningAdvice extends AfterAdvice {void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;
}
2.3 ThrowsAdvice接口
public interface ThrowsAdvice extends AfterAdvice {}
2.4 MethodInterceptor接口
public interface Interceptor extends Advice {}public interface MethodInterceptor extends Interceptor {//invoke方法内部会调用代理的那个方法Object invoke(MethodInvocation invocation) throws Throwable;
}
Spring源码:Advice接口相关推荐
- Spring源码 - getObjectForBeanInstance()
# Spring源码 - getObjectForBeanInstance() Spring版本:Spring 5.3.13-release # 1.getObjectForBeanInstance( ...
- Spring源码分析【0】-框架的基础:继承和接口调用链
Spring源码大量的使用继承和接口调用,现举个例子,不搞清楚这个无法看代码. public class A extends B{public void f1() {System.out.printl ...
- Spring源码解析 - AbstractBeanFactory 实现接口与父类分析
2019独角兽企业重金招聘Python工程师标准>>> 我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegi ...
- Spring源码分析——资源访问利器Resource之接口和抽象类分析
从今天开始,一步步走上源码分析的路.刚开始肯定要从简单着手.我们先从Java发展史上最强大的框架--Spring...旗下的资源抽象接口Resource开始吧. 我看了好多分析Spring源码的,每每 ...
- 源码解析:Spring源码解析笔记(五)接口设计总览
本文由colodoo(纸伞)整理 QQ 425343603 Java学习交流群(717726984) Spring解析笔记 启动过程部分已经完成,对启动过程源码有兴趣的朋友可以作为参考文章. 源码解析 ...
- spring源码学习之路---深入AOP(终)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...
- Spring源码-AOP(六)-自动代理与DefaultAdvisorAutoProxyCreator
2019独角兽企业重金招聘Python工程师标准>>> Spring AOP 源码解析系列,建议大家按顺序阅读,欢迎讨论 Spring源码-AOP(一)-代理模式 Spring源码- ...
- spring源码分析第五天------springAOP核心原理及源码分析
spring源码分析第五天------springAOP核心原理及源码分析 1. 面向切面编程.可以通过预 编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术 切面(A ...
- Spring 源码分析(三) —— AOP(五)创建代理
2019独角兽企业重金招聘Python工程师标准>>> 创建代理 代理的定义其实非常简单,就是改变原来目标对象方法调用的运行轨迹.这种改变,首先会对这些方法进行拦截,从而为这些方法提 ...
- Spring 源码分析(三) —— AOP(二)Spring AOP 整体架构
2019独角兽企业重金招聘Python工程师标准>>> Spring AOP 架构 先是生成代理对象,然后是拦截器的作用,最后是编织的具体实现.这是AOP实现的三个步 ...
最新文章
- web架构设计经验分享
- MySQL 5.7.17 VS MariaDB 10.1.19 Enterprise性能压测
- 网站跳出率是SEO优化转化低的诱因之一
- 【科技金融】互金欺诈黑话大集锦
- makefile中模式规则的引入和介绍------%:%.cpp
- 《操作系统真象还原》-阅读笔记(中)
- Python 如何调用 Java
- php io流 读取wav,记php中的io流---帮助理解
- MySQL中MVCC+行级锁的工作机制(源码佐证)
- 通过连接池无法连接mysql_连接池无法链接数据库
- redhat 7 oracle 11,redhat7 搭建oracle 11g RAC 问题与处理
- 安卓上传文件时修改文件名称_使用mmv命令批量修改文件名称
- 魔方栈源码-网站在线玩魔方源码-云魔方
- 误差传递公式(law of propagation of uncertainties)
- 信息流优化中,甲乙双方以及媒体的那点事
- win10计算机管理中没有本地用户和组怎么办
- 速写中的颈部肌肉怎么表现?详细画法看这里~
- Vector人工智能机器人SDK使用笔记
- java连接as400数据库,原创:用JDBC访问AS/400数据 配置与JDBC编程要点Windows系统 -电脑资料...
- 投资理财-财富的思考