理解完aop的名词解释,继续学习spring aop的工作原理。

首先明确aop到底是什么东西?又如何不违单一原则并实现交叉处理呢?

如果对它的认识只停留在面向切面编程,那就脏了。从oop(Object Oriented Programming)说起,oop引入封装,多态,继承等概念建立对象层次的结构,处理公共行为属性的集合。对于一个系统而言,需要把分散对象整合到一起的时候,oop就虚了,因为这样的需求已经在对象层次之上了。如订单模块,还款模块都需要User对象配合(当然不止于User对象完成的模块),就需要UserService接口声明的userServ对象配合来处理该模块涉及User方面的工作,此时此刻就是面向UserServ接口编程。试想,如果没有UserServ接口,那么系统各处涉及到User某一方面的工作就要相关的方法来处理,那么重复的代码量可是太凶了~所以aop在系统中把某一类对象的功能抽象为一个接口。

参考这个逗比的栗子:http://blog.csdn.net/udbnny/article/details/5870076
那睡觉这件事来理解Spring aop,睡觉是我们关心的事,但是睡觉之前和起床之前都要做一些辅助的事情,如睡觉前脱衣,起床前穿衣。

首先需要一个睡觉的接口,这个接口谁都可以谁,人、机器、大象。。

public interface Sleepable {
public void sleep();
}

然后Human来实现这个接口

public class Humman implements Sleepable {
public void sleep() {
System.out.println("梦中自有颜如玉!碎觉~ xxoxx");
}
}

睡觉的辅助接口(脱衣服接口等。。)

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("睡醒先穿衣服~");
}
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("睡觉先脱衣服~");
}
}

简单粗暴的组件都准备好了,怎么样通过Spring配置文件将他们组合起来呢?在配置文件中继续理解。

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 俺们的bean -->
<bean id="sleepHelper" class="test.spring.aop.bean.SleepHelper"></bean>
<bean id="human" class="test.spring.aop.bean.Humman"></bean>
<!-- 配置pointcut 切点相当于事故发生的地点 睡觉的这个动作在系统的什么地方出发的呢?-->
<bean id="sleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sleep"></property>
</bean>
<!-- 通知 通知相当于事故时间及内容,连接切点和通知 sleepAdvisor这个色鬼准备把某个human睡觉这件事尽收眼底。。 -->
<bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="sleepHelper"></property>
<property name="pointcut" ref="sleepPointcut"></property>
</bean>
<!-- 调用ProxyFactoryBean产生代理对象 现在我要搞清楚到底都发生了什么:human作为当事人,不好意思说话;sleepAdvisor是搞清睡觉这件事的关键,因为他看到了一切;包括睡觉以外的动作(proxyInterfaces) -->
<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="human"></property>
<property name="interceptorNames" value="sleepHelperAdvisor"></property>
<property name="proxyInterfaces" value="test.spring.aop.bean.Sleepable"></property>
</bean> </beans>

我已经搞清楚睡觉的来龙去脉了,还原下事情的经过。。

public class Test {
public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
Sleepable sleeper = (Sleepable)appCtx.getBean("humanProxy");
sleeper.sleep();
}
}

其实我已经知道结果了:

睡觉先脱衣服~

梦中自有颜如玉!碎觉~ xxoxx

睡醒先穿衣服~

Spring aop 原始的工作原理的理解的更多相关文章

  1. Spring AOP的底层实现原理

    Spring的两大核心之一就是AOP,AOP:面向切面编程.在说原理之前,得先知道一些 AOP的专业术语. AOP的专业术语 连接点(JoinPoint):增强执行的位置(增加代码的位置),Sprin ...

  2. Spring Aop之Cglib实现原理详解

    Spring Aop实现对目标对象的代理,AOP的两种实现方式:Jdk代理和Cglib代理.这两种代理的区别在于,Jdk代理与目标类都会实现同一个接口,并且在代理类中会调用目标类中被代理的方法,调用者 ...

  3. Spring MVC中DispatcherServlet工作原理探究

    转:http://blog.csdn.net/zhouyuqwert/article/details/6853730 下面类图将主要的类及方法抽离出来,以便查看方便,根据类的结构来说明整个请求是如何工 ...

  4. Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现

    前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...

  5. Spring AOP 实现原理与 CGLIB 应用

    https://www.ibm.com/developerworks/cn/java/j-lo-springaopcglib/ AOP(Aspect Orient Programming),也就是面向 ...

  6. 5.2 Spring5源码--Spring AOP源码分析二

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  7. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  8. Spring AOP中的动态代理

    0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  Spring AOP中的动态代理机制 2.1  ...

  9. 转:Spring AOP中的动态代理

    原文链接:Spring AOP中的动态代理 0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  S ...

随机推荐

  1. Texture Filter中的Bilinear、Trilinear以及Anistropic Filtering

    1. 为什么在纹理采样时需要texture filter(纹理过滤)?我们的纹理是要贴到三维图形表面的,而三维图形上的pixel中心和纹理上的texel中心并不一至(pixel不一定对应texture ...

  2. Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号

    www.cnblogs.com/zhangzhu/archive/2013/08/22/3274831.html 如何开启MySQL的远程帐号-1)首先以 root 帐户登陆 MySQL 在 Wind ...

  3. c# NPOI 导出EXCEL

    需要引入dll文件 也可以在NuGet里面管理(推荐) 比较方便 . using System; using System.Collections.Generic; using System.Linq ...

  4. linux命令(7):mv命令

    mv命令 mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] ...

  5. Ubuntu:我不小心把/var/lock文件夹给删了

    在一个风和日丽的下午,不正常关闭minicom导致了device 没有正常解锁,于是使用minicom的时候提示 device is locked: 根据网上看到的方法只要把/var/lock 里面的 ...

  6. linux whoami命令

    whoami显示的是当前"操作用户"的用户名.

  7. .NET重载运算符

    代码如下: /// <summary> /// 坐标(结构类型) /// </summary> public struct Coordinate { public int x; ...

  8. Light OJ 1032 - Fast Bit Calculations(数学)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1032 题目大意:一个十进制数变化为二进制,那么对于这个数,如果连着两个二进制位 ...

  9. convas demo1

    1 getContext 语法 Canvas.getContext(contextID) 参数 参数 contextID 指定了您想要在画布上绘制的类型.当前唯一的合法值是 "2d" ...

  10. 互联网商业模式O2O、C2C、B2B、B2C等介绍

    O2O是online to offline分为四种运营模式: 1.online to offline是线上交易到线下消费体验 2.offline to online是线下营销到线上交易 3.offli ...