理解完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. springmvc spring mybatis插入mysql中文乱码

    springmvc 插入mysql数据库中文乱码问题: 1.将页面中的编码改成utf-8 2.用SQLyog右击->改变数据库 以上两步可以保证页面数据编码一致 3.在mybatis连接的地方加 ...

  2. completed solution matches microsoft sequential workflow tutorial

    microsoft sequential workflow tutorial website:http://msdn.microsoft.com/en-us/library/ms734794(v=vs ...

  3. 初学JQuery笔记

    extend()函数是jQuery的基础函数之一,作用是扩展现有的对象 <script type="text/javascript" src="jquery-1.5 ...

  4. VS2013无法加载JSON格式

    js中通过$.getJSON加载.json 文件时,报如下错误: 解决方案:dos命令中打开 IIS Express  ,执行如下命令: appcmd set config /section:stat ...

  5. sharepoint webpart

    SharePoint开发中,不仅仅是WebPart,我们都经常会使用的几个关键位置,如下: GAC: C:\Windows\assembly,也就是部署的位置: ISAPI位置,SharePoint ...

  6. VB.net 调用dll

    Public Declare Function APlayer_OpenA Lib "APlayerCaller.dll" (ByVal aplayer As Integer, B ...

  7. MVC 基础和增删改、登录

    一.什么是MVC?1.了解MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式:       Model(模型)表示应用程序核心 ...

  8. Cannot find or open the PDB file问题的解决

    'testcv.exe' (Win32): Loaded 'D:\Documents\Visual Studio 2013\Projects\testcv\x64\Debug\testcv.exe'. ...

  9. 【python】函数之内置函数

    Python基础 内置函数 今天来介绍一下Python解释器包含的一系列的内置函数,下面表格按字母顺序列出了内置函数: 下面就一一介绍一下内置函数的用法: 1.abs() 返回一个数值的绝对值,可以是 ...

  10. 关于CMFCPropertyGridFontProperty的赋值问题

    CMFCPropertyGridFontProperty是派生于CMFCPropertyGridProperty类的用于字体设置的类.它可以设置字体的名称.大小.粗细等各项参数.但是类并不提供用于初始 ...