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,几个文件和上一篇一样 ...
随机推荐
- sanBox部署简介
参考资料:1 http://www.kaaproject.org/getting-started/ 此链接告诉我们部署sandbox的两种方法. 2 http://docs.kaaprojec ...
- [转]jQuery.validate插件在失去焦点时执行验证代码
转:http://my.oschina.net/enyo/blog/311566 关于 jquery.validate.js 表单验证插件如何在失去焦点时做验证.看手册后发现默认是在表单提交时执行验证 ...
- NET中的规范标准注释(二) -- 创建帮助文档入门篇
一.摘要 在本系列的第一篇文章介绍了.NET中XML注释的用途, 本篇文章将讲解如何使用XML注释生成与MSDN一样的帮助文件.主要介绍NDoc的继承者:SandCastle. 二.背景 要生成帮助文 ...
- 超实用的JavaScript技巧及最佳实践
众所周知,JavaScript是一门非常流行的编程语言,开发者用它不仅可以开发出炫丽的Web程序,还可以用它来开发一些移动应用程序(如PhoneGap或Appcelerator),它还有一些服务端实现 ...
- 【56测试】【字符串】【dp】【记忆化搜索】【数论】
第一题:神秘大门 大意: 两个字符串A,B,按字典序最大的顺序输出B 的每个字符在A 中的位置,如果B不全在A中,输出No,否则Yes. 解: 这道题就是一遍的扫描,因为要按字典序最大的输出,所以从后 ...
- volatile简介
volatile简介 java语言提供了一种稍弱的内存同步机制,即volatile变量.用来确保将变量的更新操作通知到其它线程,保证了新值能立即同步到主内存,以及每次使用前立即从内存刷新.当变量声明为 ...
- 读取配置文件 PropertyPlaceholderConfigurer 的配置与使用
public class SpringPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map< ...
- Invalid byte 3 of 3-byte UTF-8 sequence
用maven编译,tomcat启动时报错:IOException parsing XML document from class path resource [applicationContext.x ...
- 从0到1---“保多多”APP的开发(一)
2015年8月份,我正式接手了公司保多多APP的开发(和另一个同事一起). 我之前并没有过从0开始创建一款APP,这次能有这样的机会,实在让我感到兴奋.因为我相信,作为这款APP的主要开发人员,在这一 ...
- Spark随笔(一):Spark的综合认识
一.Spark与Hadoop的关系 Spark和Hadoop只是共用了底层的MapReduce编程模型,即它们均是基于MapReduce思想所开发的分布式数据处理系统. Hadoop采用MapRedu ...