Spring学习之AOP
Spring-AOP(Aspect-orented programming)
在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutting concerns独立出来为一个对象,这样的特殊对象称为Aspect
Aspect,即方面。所谓方面,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。
AOP代表的是一个横向的关系,如果说“对象”是一个空心的圆柱体,其中封装的是对象的属性和行为;那么面向方面编程的方法,就仿佛一把利刃,将这些空心圆柱体剖开,以获得其内部的消息。而剖开的切面,也就是所谓的“方面”了。然后它又以巧夺天功的妙手将这些剖开的切面复原,不留痕迹。
方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。事务管理是J2EE应用中一个很好的横切关注点例子。方面用Spring的 Advisor或拦截器实现。
通知(Advice): 在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。通知类型将在下面讨论。许多AOP框架包括Spring都是以拦截器做通知模型,维护一个“围绕”连接点的拦截器链。Spring中定义了四个advice: BeforeAdvice, AfterAdvice, ThrowAdvice和DynamicIntroductionAdvice
切入点(Pointcut):指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点:例如,使用正则表达式。Spring定义了Pointcut接口,用来组合MethodMatcher和ClassFilter,可以通过名字很清楚的理解, MethodMatcher是用来检查目标类的方法是否可以被应用此通知,而ClassFilter是用来检查Pointcut是否应该应用到目标类上
实现方式:


1.使用Spring API实现
通知类型:前置通知、异常通知、后置通知、环绕通知、最终通知
后置通知:
public class LogAfter implements AfterReturningAdvice{
/**
* 目标方法执行后执行的通知
* @param returnValue——返回值
* @param method 被调用方法对象
* @param args 被调用的方法的参数
* @param target 被调用的方法对象的目标对象
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行——后");
}
}
LogAfter.java
前置通知:
public class LogBefore implements MethodBeforeAdvice{
/**
* @param method 被调用方法对象
* @param args 被调用的方法的参数
* @param target 目标对象
*/
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行——前");
}
}
LogBefore
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 声明一个切面 -->
<bean id="logBefore" class="com.zhengbin.log.LogBefore" />
<bean id="logAfter" class="com.zhengbin.log.LogAfter" /> <bean id="userService" class="com.zhengbin.service.UserServiceImpl"/>
<aop:config>
<!-- pointcut切入点 -->
<!-- execution(*[表示所有返回值] com.zhengbin.service[位置](..[表示所有参数])) -->
<aop:pointcut id="pointcut" expression="execution(* com.zhengbin.service.*.*(..))" />
<!-- -->
<aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/>
</aop:config>
</beans>
beans.xml
2.自定义类实现
public class Log {
public void before(){
System.out.println("执行前");
}
public void after(){
System.out.println("执行后");
}
}
Log.java
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 声明一个切面 -->
<bean id="log" class="com.zhengbin.log.Log"/>
<bean id="userService" class="com.zhengbin.service.UserServiceImpl"/>
<aop:config>
<aop:aspect ref="log">
<aop:pointcut expression="execution(* com.zhengbin.service.*.*(..))" id="pointCut"/>
<aop:before method="before" pointcut-ref="pointCut"/>
<aop:after method="after" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
</beans>
beans.xml
3.通过注解实现
启用Spring对@AspectJ的支持:
<aop:aspectj-autoproxy/>
@Aspect
public class Log {
@Before("execution(* com.zhengbin.service.*.*(..))")
public void before(){
System.out.println("执行前");
}
@After("execution(* com.zhengbin.service.*.*(..))")
public void after(){
System.out.println("执行后");
}
}
Log.java
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 声明一个切面 -->
<bean id="log" class="com.zhengbin.log.Log"/> <bean id="userService" class="com.zhengbin.service.UserServiceImpl"/> <!-- 启用Spring对@AspectJ的支持 -->
<aop:aspectj-autoproxy/>
</beans>
beans.xml
Spring学习之AOP的更多相关文章
- Spring学习之AOP的实现方式
Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- spring学习(二) ———— AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- Spring学习之Aop的各种增强方法
AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...
- Spring学习之Aop的基本概念
转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...
- Spring学习之AOP与事务
一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...
- spring学习笔记-AOP
1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用: 提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...
- Spring 学习之AOP
1. 走进面前切面编程 编程范式: 面向过程编程,c语言: 面向对象编程:c++,java,c#; 函数式编程: 事件驱动编程: 面向切面编程: AOP是一种编程范式,不是编程语言:解决特定问题,不能 ...
- Spring 学习二-----AOP的原理与简单实践
一.Spring AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...
随机推荐
- 几款实用的 JavaScript 图形图表库
一款好的图表插件不是那么容易找到的.最近项目里需要实现统计图表功能,所以在网上搜罗了一圈,找到一些不错的图表插件,分享大家.众多周知,图形和图表要比文本更具表现力和说服力.这里给大家精心推荐几款实用的 ...
- Searching a 2D Sorted Matrix Part I
Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...
- iframe标签用法详解(属性、透明、自适应高度)(总结)
<iframe src="http://www.jb51.net" width="200" height="500"> 脚本之家 ...
- java基础知识回顾之java Socket学习(一)--UDP协议编程
UDP传输:面向无连接的协议,不可靠,只是把应用程序传给IP层的数据报包发送出去,不保证发送出去的数据报包能到达目的地.不用再客户端和服务器端建立连接,没有超时重发等机制,传输速度快是它的优点.就像寄 ...
- ***iOS 项目的目录结构能看出你的开发经验
最近有师弟去面试iOS开发,他谈论到,面试官竟然问他怎么分目录结构的,而且还具体问到每个子目录的文件名. 目录结构确实很重要,面试官问他这些无疑是想窥探他的开发经验.清晰的目录结构,可让人一眼知道对应 ...
- StringUtils.isNumeric使用
在做导入/导出功能时,客户要求导出数字类型的值时,将excel相应单元格属性设为number型,由此需判断字符串值是否为数字,代码如下: public static boolean isNumber( ...
- linux进程的地址空间,核心栈,用户栈,内核线程
linux进程的地址空间,核心栈,用户栈,内核线程 地址空间: 32位linux系统上,进程的地址空间为4G,包括1G的内核地址空间,和3G的用户地址空间. 内核栈: 进程控制块task_struct ...
- JDK安装配置问题
JDK安装过程中会有两个安装提示,一个是jdk的安装,一个是jre的安装
- HTML5入门3---视频播放器
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- *两个关键字static和final
static关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰类. static 修饰属性: 无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态的成员变量:一个对象对该静态成员变量 ...