spring aop编程
1、AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代码混乱。拦截机 interceptor是AOP的另一中叫法。(其中使用的模式为代理模式,动态代理模式)。
2、通知advise:在aspect的某个连接点(在Spring的aop中,一个连接点就是一个方法的执行)上执行的动作。
切入点pointcut:匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行。
目标对象:target object,被一个活多个切面所通知的对象。也称被通知对象。
3、spring中的aop
加入支持aop的jar包:
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
spring-aop.jar
spring-aspect.jar
(1)在xml中加入aop的命名空间:
<?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: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"> </beans>
(2)写aspect类和切面的通知
<bean id="myAspect" class="com.spring1.util.Aspect"/>
<aop:config>
<!--切面 -->
<aop:aspect ref="myAspect">
<!-- 切入点 执行(方法访问修饰符? 方法返回类型 声明类? 方法名(方法参数类型) 抛出异常?) -->
<aop:pointcut expression="execution(* com.spring1.dao..*.*(..))" id="point1"/>
<aop:before method="beforeAdvise" pointcut-ref="point1"/>
<aop:after method="afterAdvise" pointcut-ref="point1"/>
<!-- 返回通知,最后的参数是通知参数的名 -->
<aop:after-returning method="afterReturning" pointcut-ref="point1" returning="ret"/>
<!-- 异常通知,最后参数是异常的参数的名 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="point1" throwing="e"/>
</aop:aspect>
</aop:config>
(3)将aspect类交给Spring管理,配置aspect类的bean
上边已配置过
(4)根据aop中的通知写通知函数:
//切面类
public class Aspect {
//通知...
void beforeAdvise(){
System.out.println("before");
} void afterAdvise(){
System.out.println("after");
} void afterReturning(Object ret){
System.out.println("afterReturning " +ret.toString());
} void afterThrowing(Exception e){
System.out.println("afterThrowing " + e.getMessage());
}
}
(5)当上边的都需要些的时候可以直接用 环绕通知代替:
<bean id="myAspect" class="com.spring1.util.Aspect"/>
<aop:config>
<!--切面 -->
<aop:aspect ref="myAspect">
<!-- 切入点 执行(方法访问修饰符? 方法返回类型 声明类? 方法名(方法参数类型) 抛出异常?) -->
<aop:pointcut expression="execution(* com.spring1.dao..*.*(..))" id="point1"/>
<aop:around pointcut-ref="point1" method="aroundAdvise"/>
</aop:aspect>
</aop:config>
void aroundAdvise(ProceedingJoinPoint pjp){
try {
System.out.println("前置");
Object object = pjp.proceed();
System.out.println(object.toString());
} catch (Throwable e) {
e.printStackTrace();
System.out.println("异常");
}finally{
System.out.println("最终");
}
}
后置通知就是返回后的通知
最终通知又叫finally通知。
spring aop编程的更多相关文章
- Spring AOP编程(二)-AOP实现的三种方式
AOP的实现有三种方式: l aop底层将采用代理机制进行实现. l 接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l 实现类: ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- 二)Spring AOP编程思想与动态代理
一.aop编程思想 1.面向切面,就是能够不动源码的情况下,从横切面切入新的代码功能. 2.实现原理是动态代理 动态代理的步骤 a.写生产厂家,实现接口,代理只能代理接口 b.动态代理类实现Invoc ...
- Spring AOP编程经验总结
编程范式概览:面向过程,面向对象,函数式编程,事件驱动编程,面向切面等, AOP是什么? Spring AOP是采用面向切面编程的编程范式,而非编程语言,它只能解决特定问题,而非所有问题,它与OOP不 ...
- Spring AOP编程(一)-AOP介绍
1. AOP介绍 l 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...
- spring AOP编程--AspectJ注解方式
1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...
- (转)Spring AOP编程原理、Demo
转自: http://pandonix.iteye.com/blog/336873/ AOP原理: http://blog.csdn.net/moreevan/article/details/1197 ...
- spring AOP 编程--AspectJ注解方式 (4)
1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...
- spring切面编程AOP 范例一
参照网上的spring AOP编程实例进行配置,但是碰到了几个坑.这篇文章重点讲解一下我踩过的两个坑: 1.使用@Service自动装配的时候,基础扫描包配置要正确: 2.xml中切面配置中的exec ...
随机推荐
- iOS - Swift NSEnumerator 迭代器
前言 public class NSEnumerator : NSObject, NSFastEnumeration 1.迭代器 let arr:NSArray = ["bei", ...
- HIHO 线段树(单点)
#include <stdio.h> #include <string.h> #include <math.h> #include <iostream> ...
- 图片在父元素中上下居中(vertical-align的有效性)
在实际的使用中,会遇到img上下居中的问题: 1.一般情况下,将其放置在table中:可以自动的上下居中. 2.另外一种情况<即一般情况下> 以li中为例子:在无序列表中 li元素下的 ...
- shiro-web整合
1.所需要jar <!-- shiro核心包 --> <dependency> <groupId>org.apache.shiro</groupId> ...
- sscanf的用法(转)
队长做上海邀请赛的I题时遇到一个棘手的问题,字符串的处理很麻烦,按传统的gets全部读入的话还要做N多处理,太浪费时间. 回来之后搜了一下sscanf的用法发现可以很好的解决这一类问题,各种百度,转来 ...
- mysql 事件调度器
1.mysql事件调度器,也就是计划任务,计划做某事,有两种方式: 2.在某个时间点做某事,AT TIMESTAMP [+ INTERVAL INTERVAL] 某个时间点加上偏移. 3.定时地做某事 ...
- UIButton、UIImageView、UILabel的选择
UIButton特点既能显示文字,又能显示图片(能显示2张图片,背景图片.内容图片)长按高亮的时候可以切换图片\文字直接通过addTarget...方法监听点击 UIImageView能显示图片,不能 ...
- Android热修复
https://github.com/WeMobileDev/article/blob/master/%E5%BE%AE%E4%BF%A1Android%E7%83%AD%E8%A1%A5%E4%B8 ...
- 这个算asp.net的一个bug吗?
asp.net设置按钮Enabled="false"后OnClientClick中添加的验证脚本消失了 下面的确可以 <asp:Button ID="btnRegi ...
- Android事件传递机制(转)
Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和多指操作.所有这些都构成了Android中的事件响应.总的来说,所有的事件都 ...