Spring AOP 简单理解
AOP技术即(面向切面编程)技术是在面向对象编程基础上的发展,AOP技术是对所有对象或一类对象编程。核心是在不增加代码的基础上,还增加了新的功能。AOP编程在开发框架本身用的比较多,而实际项目中,用的比较少。它是将分散在各个业务逻辑代码中的相同代码抽取出来形成一个独立的模块。
1、定义AOP术语
(1)切面(aspect):要实现的交叉功能,是系统模块化的一个切面或领域。
(2)通知(advice):切面的具体实现,包含五类通知。
(3)连接点(jointpoint):应用程序执行过程中插入切面的地点。
(4)切点(cutpoint):定义通知应该应用哪些连接点。
(5)引入(introduction):为类添加新方法和属性。
(6)目标对象(target):通知逻辑的织入目标类。
(7)代理(proxy):将通知应用到目标对象后创建的对象,应用系统的其他部分不用为了支持代理对象而改变
(8)织入(weaving):将切面应用到目标对象从而创建一个新代理对象的过程。
2、AOP原理和实例
(1)基础接口和类的实现:
- -May-2012 18:19:53 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- /0;
- System.out.println("sayHi() method");
- }
- }
执行结果如下:
- INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ce2dd4: defining beans [testService,myMethodBeforeAdvice,myAfterReturningAdvice,myMethodInterceptor,myThrowsAdvice,proxyFactoryBean]; root of factory hierarchy
- 前置通知调用 记录日志...sayHello
- 环绕通知调用方法前
- sayHello() method
- 环绕通知调用方法后
- 后置通知调用,关闭资源...sayHello
- *******************************************
- 前置通知调用 记录日志...sayBye
- 环绕通知调用方法前
- sayBye() method
- 环绕通知调用方法后
- 后置通知调用,关闭资源...sayBye
- *******************************************
- 前置通知调用 记录日志...sayHi
- 环绕通知调用方法前
- 异常通知产生异常,进行处理/ by zero
- Exception in thread "main" java.lang.ArithmeticException: / by zero
(5)上面的通知都是针对每个方法的,如果只是对单个或者一类的方法进行相应处理的时,可采用名字或者正则表达式的方式进行处理
配置如下:
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
- >
- <!-- 配置被代理的对象,即目标对象 -->
- <bean id="testService" class="com.jasson.aop.TestService" />
- <!-- 配置前置通知 -->
- <bean id="myMethodBeforeAdvice" class="com.jasson.aop.MyMethodBeforeAdvice" />
- <!-- 配置后置通知 -->
- <bean id="myAfterReturningAdvice" class="com.jasson.aop.MyAfterReturningAdvice" />
- <!-- 配置环绕通知 -->
- <bean id="myMethodInterceptor" class="com.jasson.aop.MyMethodInterceptor" />
- <!-- 配置异常通知 -->
- <bean id="myThrowsAdvice" class="com.jasson.aop.MyThrowsAdvice" />
- <!-- 通知与正则表达式切入点一起配置 -->
- <!-- Advisor等于切入点加通知,所有say开头的方法添加前置通知 -->
- <bean id="regexpPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
- <property name="advice" ref="myMethodBeforeAdvice"/>
- <property name="patterns">
- <list>
- <value>.*say.*</value>
- </list>
- </property>
- </bean>
- <!-- 方法名匹配切入点配置器:只对 sayHello方法添加环绕通知-->
- <bean id="namePointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
- <property name="advice" ref="myMethodInterceptor"/>
- <property name="mappedNames">
- <list>
- <value>sayHello</value>
- </list>
- </property>
- </bean>
- <!-- 配置代理对象 -->
- <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
- <!-- 代理接口集 -->
- <property name="proxyInterfaces">
- <list>
- <value>com.jasson.aop.TestServiceInter1</value>
- <value>com.jasson.aop.TestServiceInter2</value>
- </list>
- </property>
- <!-- 把通知织入到代理对象 -->
- <property name="interceptorNames">
- <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们也
- 可以把通知看出拦截器,struts2核心拦截器 -->
- <list>
- <value>namePointcutAdvisor</value>
- <value>myAfterReturningAdvice</value>
- <value>regexpPointcutAdvisor</value>
- <value>myThrowsAdvice</value>
- </list>
- </property>
- <!-- 配置被代理对象,即目标对象 -->
- <property name="target" ref="testService"/>
- </bean>
- </beans>
执行结果如下:
- 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
- 环绕通知调用方法前
- 前置通知调用 记录日志...sayHello
- sayHello() method
- 后置通知调用,关闭资源...sayHello
- 环绕通知调用方法后
- *******************************************
- 前置通知调用 记录日志...sayBye
- sayBye() method
- 后置通知调用,关闭资源...sayBye
- *******************************************
- 前置通知调用 记录日志...sayHi
- 异常通知产生异常,进行处理/ by zero
- Exception in thread "main" java.lang.ArithmeticException: / by zero
Spring AOP 简单理解的更多相关文章
- spring aop简单理解
aop原理是spring帮我们封装了动态代理,然后我们只管写具体的业务,我们将公共业务也写到具体的一个类中并实现spring为我们提供的对应要连接切入哪个位置的接口,然后再xml中配置它们的关系即可. ...
- Spring AOP深入理解之拦截器调用
Spring AOP深入理解之拦截器调用 Spring AOP代理对象生成回想 上一篇博客中:深入理解Spring AOP之二代理对象生成介绍了Spring代理对象是怎样生成的,当中重点介绍了JDK动 ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- Spring Aop的理解和简单实现
1.AOP概念 所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合.比如连接数据库来说: 加载驱动-----获取class--------获取连接对象-------访问数 ...
- Spring AOP 简单入门笔记 (转)
分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...
- Spring aop 简单示例
简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出了一下记录 首先看下整个例子的目 ...
- spring aop 的理解
spring aop的相关概念(所有的概念都是为了生成代理类这个过程所需要的信息的抽象): 1.Targer:目标对象.被代理的对象. 2.Advice:增强/通知.就是为目标对象扩展的功能.分为前置 ...
- Spring的AOP简单理解
最近在研究spring的AOP,翻译出来的意思是面向切面. 总结如下: 所谓AOP就是将分散在各个方法处的公共代码提取到一处, 并通过类似拦截器的机制实现代码的动态整合.可以简单地想象成, 在某个方法 ...
- spring AOP简单入门
AOP(aspect oriented programming)面向切面编程. 大致意思是在方法的执行过程中织入其他要执行的方法. 项目结构图 先介绍一下通过代理的方式实现aop,几个文件和上一篇一样 ...
随机推荐
- break和continue的区别
break是结束整个循环体,continue是结束单次循环
- sass学习笔记2
今天介绍sass在重用代码时最具威力的两个功能.一个是嵌套(Nesting),一个混合(Mixin). 我们在写CSS通过需要多个后代选择器组合到一起才能定位到目标元素上,而这定义过程,此元素的父元素 ...
- flash透明效果代码分享~~~
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- angular directive指令内的参数
angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...
- eclipse改变theme
https://github.com/eclipse-color-theme/eclipse-color-theme.git https://github.com/eclipse-color-them ...
- 使用 Hive 作为 ETL 或 ELT 工具
用来处理数据的 ETL 和 ELT 工具的概述 数据集成和数据管理技术已存在很长一段时间.提取.转换和加载(ETL)数据的工具已经改变了传统的数据库和数据仓库.现在,内存中转换 ETL 工具使得提取. ...
- 在chrome console加入jquery库
var jq = document.createElement('script'); jq.src = 'http://libs.baidu.com/jquery/1.9.1/jquery.min.j ...
- [强连通分量] 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 ...
- NetworkReachable学习笔记
一.基本知识 在需要联网的iPhone程序中,我们首先需要检查网络的状态,如果不能连接网络则告诉用户程序不能使用的原因是没有网络连接.在iPhone的SystemConfiguration框架里有提供 ...
- loadrunner (一)如何查看分析、报告结果