Spring AOP体系学习总结:
要理解AOP整体的逻辑需要理解一下Advice,Pointcut,Advisor的概念以及他们的关系。
Advice是为Spring Bean提供增强逻辑的接口,提供了多种方法增强的方式,比如前置,后置,包裹等增强方式。看下Advice的类体系结构图:
图中定义了主要有3中类型的Advice,分别是BeforeAdvice,AfterAdvice 和 Interceptor,BeforeAdvice就是定义的就是方法的前置织入逻辑,AfterAdvice就是方法的后置织入逻辑。MethodInteceptor定义的是方法的包裹逻辑。想要分析其原理,先要看看怎么用,看一个应用的DEMO:
AfterAdvice.class:
public class AfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("这个是 AfterReturning 方法!");
}
}
BeforeAdvice.class:
public class BeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("这是BeforeAdvice的before方法!");
}
}
CompareInterceptor.classpublic class CompareInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = null;
String stu_name = invocation.getArguments()[0].toString();
if ( stu_name.equals("dragon")){
//如果学生是dragon时,执行目标方法, result= invocation.proceed();
} else{
System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
}
return result;
}
}
以上定义的分别是目标方法的前置逻辑,后置逻辑,及包裹逻辑。
目标类接口:
public interface IStudent { public void addStudent(String name);
}
目标实现类:
public class StudentImpl implements IStudent { @Override public void addStudent(String name) {
System.out.println(name);
}
}
Bean定义的配置文件:
<beans>
<bean id="beforeAdvice" class="aop.demo.demo1.BeforeAdvice"></bean>
<bean id="afterAdvice" class="aop.demo.demo1.AfterAdvice"></bean>
<bean id="compareInterceptor" class="aop.demo.demo1.CompareInterceptor">
</bean><bean id="studenttarget" class="aop.demo.demo1.StudentImpl"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>aop.demo.demo1.IStudent</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
</property>
</bean>
</beans>
测试驱动类:<br>
public class DriverTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/main/java/aop/demo/applicationContext.xml");
IStudent person = (IStudent)ctx.getBean("student");
//person.addStudent("dragon");
person.addStudent("javadragon");
}
}
//执行结果:<br>
这是BeforeAdvice的before方法!
此学生是javadragon而不是dragon,不批准其加入.
这个是 AfterReturning 方法!
Spring AOP体系学习总结:的更多相关文章
- Spring AOP体系学习总结
要理解AOP整体的逻辑需要理解一下Advice,Pointcut,Advisor的概念以及他们的关系. Advice是为Spring Bean提供增强逻辑的接口,提供了多种方法增强的方式,比如前置, ...
- spring AOP的学习
1.Spring常用的概念 Joinpoint(连接点): 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. Pointcut(切入点): ...
- 笔记-spring aop 原理学习2
InstantiationAwareBeanPostProcessor AnnotationAwareAspectJAutoProxyCreator https://blog.csdn.net/qq_ ...
- spring aop 原理学习
@EnableAspectJAutoProxy: @Import(AspectJAutoProxyRegistrar.class) 实际是创建了一个以org.springframework.aop.c ...
- 关于spring AOP的学习
比较好的帖子http://www.cnblogs.com/xing901022/p/4265544.html
- Spring AOP 知识整理
通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...
- 【目录】Spring 源码学习
[目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
随机推荐
- [topcoder]IncreasingSubsequences
http://community.topcoder.com/stat?c=problem_statement&pm=7753&rd=10672 http://community.top ...
- 【HDOJ】2544 最短路
Dijkstra. #include <stdio.h> #include <string.h> #define INF 0xfffffff ][]; ]; ]; int ma ...
- POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)
描述 http://poj.org/problem?id=2739 多次询问,对于一个给定的n,求有多少组连续的素数,满足连续素数之和为n. Sum of Consecutive Prime Numb ...
- [转]笔记本Ubuntu系统关闭独显+省电降温设置
[转载者按]最近装了Ubuntu 13.04 64 bits版操作系统玩玩,但是发现两个显卡都开着,所以上网查找资料,以在不需要3D的时候关闭Nvidia显卡.通过Bumblebee软件包可以达到这一 ...
- HDU 2546 饭卡
http://acm.hdu.edu.cn/showproblem.php?pid=2546 呆呆. 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memo ...
- vs2010 更新jQuery智能提示包
vs2010 更新jQuery只能提示包时,可以直接在NuGet中更新 jquery-2.1.0-vsdoc.js jquery-2.1.0.js jquery-2.1.0.min.js jquery ...
- 深入设计模式(二)——单例模式(Singleton Pattern)
一.单例模式介绍 单例模式(Singleton Pattern),保证一个类只有一个实例,并提供一个访问它的全局访问点.单例模式因为Singleton封装它的唯一实例,它就可以严格地控制客户怎样访问它 ...
- [SAM4N学习笔记]SAM4N工程模板搭建
一.需要安装的软件: 因为笔者是使用MDK-ARM开发的版本是4.72,所以需要安装这个工具,具体在哪里下载自行放狗或问度娘.除了这个重要工具以为,还需要安装Atmel官方的Atmel Studio, ...
- (转载)利用burp suite截断上传拿shell
burpsuite上传必须要有filepath这个参数 第一步:选择一个jpg后缀的马. 第二步:设置本地代理,burp的本地端口是8080 第三步:打开burp suite 按图操作就ok了. 第四 ...
- Linux I2C设备驱动编写(三)-实例分析AM3359
TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1版本规格 支持标准模式(100K bits/s)和快速模式(400K bits/s) 多路接收.发送模式 ...