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,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...
随机推荐
- spring @resource @ Autowired
Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类 ...
- Sqlitekit 封装管理
最近需要用到Sqlite数据库来做一个游戏的数据存储.网上搜了一下,两种方法,一种是自己dll搭建环境有可能还需要编译之类的,我自己是搭建出来了,不过我没采用. 还有一种就是使用sqlitekit插件 ...
- ext3grep 恢复删除
Linux ext2/ext3 文件删除恢复工具ext3grep安装使用 2010-08-23 18:03:10| 分类: 默认分类|举报|字号 订阅 一. 安装前系统环 ...
- How does database indexing work?
When data is stored on disk based storage devices, it is stored as blocks of data. These blocks are ...
- POJ 1928
#include <iostream> #include <algorithm> #define MAXN 3000 using namespace std; struct n ...
- POJ1004Financial Management
这个题犯了一个小小的错误,double输出的时候用f才对,输入用lf即可.... http://poj.org/problem?id=1004 #include<stdio.h> int ...
- DJANGO的requirements的运用
这里记录一下我现在项目的requirements.pip文件,安装命令为: pip install -r requirements.pip 这样一来,所有依赖,全部搞定. Django== djang ...
- MySQL数据导出导入【转】
MySQL基础 关于MySQL数据导出导入的文章,目的有二: 1.备忘 2.供开发人员测试 工具 mysqlmysqldump 应用举例 导出 导出全库备份到本地的目录 mysqldump -u$US ...
- 130701基础练习-first
// 629.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"//#include <iostream.h> class Point ...
- lintcode :continuous subarray sum 连续子数组之和
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, ...