AOP技术即(面向切面编程)技术是在面向对象编程基础上的发展,AOP技术是对所有对象或一类对象编程。核心是在不增加代码的基础上,还增加了新的功能。AOP编程在开发框架本身用的比较多,而实际项目中,用的比较少。它是将分散在各个业务逻辑代码中的相同代码抽取出来形成一个独立的模块。

1、定义AOP术语

(1)切面(aspect):要实现的交叉功能,是系统模块化的一个切面或领域。

(2)通知(advice):切面的具体实现,包含五类通知。

(3)连接点(jointpoint):应用程序执行过程中插入切面的地点。

(4)切点(cutpoint):定义通知应该应用哪些连接点。

(5)引入(introduction):为类添加新方法和属性。

(6)目标对象(target):通知逻辑的织入目标类。

(7)代理(proxy):将通知应用到目标对象后创建的对象,应用系统的其他部分不用为了支持代理对象而改变

(8)织入(weaving):将切面应用到目标对象从而创建一个新代理对象的过程。

2、AOP原理和实例

(1)基础接口和类的实现:

  1. -May-2012 18:19:53 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  2. /0;
  3. System.out.println("sayHi() method");
  4. }
  5. }

执行结果如下:

  1. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ce2dd4: defining beans [testService,myMethodBeforeAdvice,myAfterReturningAdvice,myMethodInterceptor,myThrowsAdvice,proxyFactoryBean]; root of factory hierarchy
  2. 前置通知调用 记录日志...sayHello
  3. 环绕通知调用方法前
  4. sayHello() method
  5. 环绕通知调用方法后
  6. 后置通知调用,关闭资源...sayHello
  7. *******************************************
  8. 前置通知调用 记录日志...sayBye
  9. 环绕通知调用方法前
  10. sayBye() method
  11. 环绕通知调用方法后
  12. 后置通知调用,关闭资源...sayBye
  13. *******************************************
  14. 前置通知调用 记录日志...sayHi
  15. 环绕通知调用方法前
  16. 异常通知产生异常,进行处理/ by zero
  17. Exception in thread "main" java.lang.ArithmeticException: / by zero

(5)上面的通知都是针对每个方法的,如果只是对单个或者一类的方法进行相应处理的时,可采用名字或者正则表达式的方式进行处理

配置如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
  9. >
  10. <!-- 配置被代理的对象,即目标对象 -->
  11. <bean id="testService" class="com.jasson.aop.TestService" />
  12. <!-- 配置前置通知 -->
  13. <bean id="myMethodBeforeAdvice" class="com.jasson.aop.MyMethodBeforeAdvice" />
  14. <!-- 配置后置通知 -->
  15. <bean id="myAfterReturningAdvice" class="com.jasson.aop.MyAfterReturningAdvice" />
  16. <!-- 配置环绕通知 -->
  17. <bean id="myMethodInterceptor" class="com.jasson.aop.MyMethodInterceptor" />
  18. <!-- 配置异常通知 -->
  19. <bean id="myThrowsAdvice" class="com.jasson.aop.MyThrowsAdvice" />
  20. <!-- 通知与正则表达式切入点一起配置 -->
  21. <!-- Advisor等于切入点加通知,所有say开头的方法添加前置通知 -->
  22. <bean id="regexpPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  23. <property name="advice" ref="myMethodBeforeAdvice"/>
  24. <property name="patterns">
  25. <list>
  26. <value>.*say.*</value>
  27. </list>
  28. </property>
  29. </bean>
  30. <!-- 方法名匹配切入点配置器:只对 sayHello方法添加环绕通知-->
  31. <bean id="namePointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  32. <property name="advice" ref="myMethodInterceptor"/>
  33. <property name="mappedNames">
  34. <list>
  35. <value>sayHello</value>
  36. </list>
  37. </property>
  38. </bean>
  39. <!-- 配置代理对象 -->
  40. <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
  41. <!-- 代理接口集 -->
  42. <property name="proxyInterfaces">
  43. <list>
  44. <value>com.jasson.aop.TestServiceInter1</value>
  45. <value>com.jasson.aop.TestServiceInter2</value>
  46. </list>
  47. </property>
  48. <!-- 把通知织入到代理对象  -->
  49. <property name="interceptorNames">
  50. <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们也
  51. 可以把通知看出拦截器,struts2核心拦截器 -->
  52. <list>
  53. <value>namePointcutAdvisor</value>
  54. <value>myAfterReturningAdvice</value>
  55. <value>regexpPointcutAdvisor</value>
  56. <value>myThrowsAdvice</value>
  57. </list>
  58. </property>
  59. <!-- 配置被代理对象,即目标对象 -->
  60. <property name="target" ref="testService"/>
  61. </bean>
  62. </beans>

执行结果如下:

  1. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ef9157: defining beans [testService,myMethodBeforeAdvice,myAfterReturningAdvice,myMethodInterceptor,myThrowsAdvice,regexpPointcutAdvisor,namePointcutAdvisor,proxyFactoryBean]; root of factory hierarchy
  2. 环绕通知调用方法前
  3. 前置通知调用 记录日志...sayHello
  4. sayHello() method
  5. 后置通知调用,关闭资源...sayHello
  6. 环绕通知调用方法后
  7. *******************************************
  8. 前置通知调用 记录日志...sayBye
  9. sayBye() method
  10. 后置通知调用,关闭资源...sayBye
  11. *******************************************
  12. 前置通知调用 记录日志...sayHi
  13. 异常通知产生异常,进行处理/ by zero
  14. Exception in thread "main" java.lang.ArithmeticException: / by zero

Spring AOP 简单理解的更多相关文章

  1. spring aop简单理解

    aop原理是spring帮我们封装了动态代理,然后我们只管写具体的业务,我们将公共业务也写到具体的一个类中并实现spring为我们提供的对应要连接切入哪个位置的接口,然后再xml中配置它们的关系即可. ...

  2. Spring AOP深入理解之拦截器调用

    Spring AOP深入理解之拦截器调用 Spring AOP代理对象生成回想 上一篇博客中:深入理解Spring AOP之二代理对象生成介绍了Spring代理对象是怎样生成的,当中重点介绍了JDK动 ...

  3. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  4. Spring Aop的理解和简单实现

    1.AOP概念 所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合.比如连接数据库来说: 加载驱动-----获取class--------获取连接对象-------访问数 ...

  5. Spring AOP 简单入门笔记 (转)

    分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...

  6. Spring aop 简单示例

    简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出了一下记录 首先看下整个例子的目 ...

  7. spring aop 的理解

    spring aop的相关概念(所有的概念都是为了生成代理类这个过程所需要的信息的抽象): 1.Targer:目标对象.被代理的对象. 2.Advice:增强/通知.就是为目标对象扩展的功能.分为前置 ...

  8. Spring的AOP简单理解

    最近在研究spring的AOP,翻译出来的意思是面向切面. 总结如下: 所谓AOP就是将分散在各个方法处的公共代码提取到一处, 并通过类似拦截器的机制实现代码的动态整合.可以简单地想象成, 在某个方法 ...

  9. spring AOP简单入门

    AOP(aspect oriented programming)面向切面编程. 大致意思是在方法的执行过程中织入其他要执行的方法. 项目结构图 先介绍一下通过代理的方式实现aop,几个文件和上一篇一样 ...

随机推荐

  1. break和continue的区别

    break是结束整个循环体,continue是结束单次循环

  2. sass学习笔记2

    今天介绍sass在重用代码时最具威力的两个功能.一个是嵌套(Nesting),一个混合(Mixin). 我们在写CSS通过需要多个后代选择器组合到一起才能定位到目标元素上,而这定义过程,此元素的父元素 ...

  3. flash透明效果代码分享~~~

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  4. angular directive指令内的参数

    angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...

  5. eclipse改变theme

    https://github.com/eclipse-color-theme/eclipse-color-theme.git https://github.com/eclipse-color-them ...

  6. 使用 Hive 作为 ETL 或 ELT 工具

    用来处理数据的 ETL 和 ELT 工具的概述 数据集成和数据管理技术已存在很长一段时间.提取.转换和加载(ETL)数据的工具已经改变了传统的数据库和数据仓库.现在,内存中转换 ETL 工具使得提取. ...

  7. 在chrome console加入jquery库

    var jq = document.createElement('script'); jq.src = 'http://libs.baidu.com/jquery/1.9.1/jquery.min.j ...

  8. [强连通分量] POJ 2762 Going from u to v or from v to u?

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17089 ...

  9. NetworkReachable学习笔记

    一.基本知识 在需要联网的iPhone程序中,我们首先需要检查网络的状态,如果不能连接网络则告诉用户程序不能使用的原因是没有网络连接.在iPhone的SystemConfiguration框架里有提供 ...

  10. loadrunner (一)如何查看分析、报告结果