要理解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体系学习总结:的更多相关文章

  1. Spring AOP体系学习总结

    要理解AOP整体的逻辑需要理解一下Advice,Pointcut,Advisor的概念以及他们的关系.  Advice是为Spring Bean提供增强逻辑的接口,提供了多种方法增强的方式,比如前置, ...

  2. spring AOP的学习

    1.Spring常用的概念 Joinpoint(连接点): 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. Pointcut(切入点): ...

  3. 笔记-spring aop 原理学习2

    InstantiationAwareBeanPostProcessor AnnotationAwareAspectJAutoProxyCreator https://blog.csdn.net/qq_ ...

  4. spring aop 原理学习

    @EnableAspectJAutoProxy: @Import(AspectJAutoProxyRegistrar.class) 实际是创建了一个以org.springframework.aop.c ...

  5. 关于spring AOP的学习

    比较好的帖子http://www.cnblogs.com/xing901022/p/4265544.html

  6. Spring AOP 知识整理

    通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...

  7. 【目录】Spring 源码学习

    [目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...

  8. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  9. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

随机推荐

  1. 文件已经加入.gitignore但是vs并没有显示文件处于ignore状态

    在VS2015的项目文件中看到某些文件的状态比较特殊, 前面被标记了红色的标志, 如下图. 本来以为这是通过VS修改文件属性做到的, 但是光标移到文件上发现显示的是Ignore, 才知道是被git所忽 ...

  2. 2.5.6 使用progressDialog创建进度对话框

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  3. BZOJ3188: [Coci 2011]Upit

    3188: [Coci 2011]Upit Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 24[Submit][Status] ...

  4. 【Android 开源】:最火的Android开源项目 第02期

    21. drag-sort-listview DragSortListView(DSLV)是Android ListView的一个扩展,支持拖拽排序和左右滑动删除功能.重写了TouchIntercep ...

  5. 一点一点学ASP.NET系列

    转自:http://www.cnblogs.com/stwyhm/archive/2006/08/10/473075.html 做开发近两年了,自认为自己还算是个知道要上进的人,每天不停地学习,不停地 ...

  6. Web三维技术:Flash Builder+away3d平台搭建(含演示视频)

    转自:http://www.cnblogs.com/beer/archive/2011/07/08/2101492.html 前言:作为页面中实验设备的显示层,需要一个swf作为显示的UI.虽然可以用 ...

  7. js怎样生成json的数据

    var row1 = {};row1.name = 'david';row1.age = '20'; //或者var row2 = {name: 'peter', age: '23'}; var da ...

  8. AudioMixer的脚本控制

    AudioMixer是Unity5新特性之一,能很好的实现立体声效果. 这儿先记录一下脚本控制的方法: 1.添加一个Group,然后点击它 2.右侧面板上出现2个参数:pitch(速度)和volume ...

  9. Js 时间轴和拓扑图

    http://code.csdn.net/news/2819345 http://visjs.org/

  10. JavaScript高级程序设计39.pdf

    第13章 事件 JavaScript与HTML之间的交互式通过事件来实现的. 事件流 事件流描述的是从页面中接收事件的顺序,IE和Netscape提出了完全相反的事件流概念,IE是事件冒泡流,Nets ...