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,几个文件和上一篇一样 ...
随机推荐
- Ubuntu12.04安装64位系统出现编译错误error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or dir
问题: Ubuntu12.04安装64位系统出现编译错误error while loading shared libraries: libz.so.1: cannot open shared obje ...
- Search Insert Position [LeetCode]
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 错误信息:System.Resources.MissingManifestResourceException: 未能找到任何适合于指定的区域或非特定区域性的资源。请确保在编译时已将“****.****.Resource.resources”正确嵌入或链接到程序集"****",或者确保所有需要的附属程序集都可加载并已进行了完全签名
在网上搜索了N久都没看到几篇解决的文章,最后在不懈的努力下终于解决了,所以决定写下解决方法方便以后遇到同样问题的朋友: 其实这个错误的主要问题就是没有找到需要的资源文件(该文件为Resources.r ...
- 对checkbox 的checked的一些总结
在做一个jquery树形结构的复选框选择的效果. 遇到的问题: 1.jquery复选框判断是否被选中 $(check).attr("checked"),可能提示为undefied: ...
- IntelliJ UI安装
- 响应式注意要添加“视口”约束标记---viewport
视口:我们试图在iPhone中输出屏幕宽度,你会发现屏幕宽度是980,却和PC屏幕差不多大.原因是苹果主导的这些手机厂商,为了使用户获得完整web体验,很多设备都会欺骗浏览器返回一个数值较大的“视口” ...
- sql server 子找父和父找子
父找子 with RTD1 as( select Id,pid from Sys_XCode ), RTD2 as( select * from RTD1 where id=1 union all s ...
- 前端获取url参数
function GetQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)( ...
- Spring与JPA
Java持久化API(Java Persistence API),即JPA Spring中使用JPA的第一步是要在Spring应用上下文中将实体管理器工厂(entity manager factory ...
- 设计模式六大原则(5)—迪米特法则
定义: 一个对象应该对其它的对象保持最少的了解.迪米特法则又称为最少知识法则,英文全称为Least Knowledge Principle ,简称为LKP. 个人理解: 迪米特法则主要目的是类间解耦, ...