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编程的更多相关文章

  1. Spring AOP编程(二)-AOP实现的三种方式

    AOP的实现有三种方式: l         aop底层将采用代理机制进行实现. l         接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l         实现类: ...

  2. Spring学习笔记之四----基于Annotation的Spring AOP编程

    你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...

  3. 二)Spring AOP编程思想与动态代理

    一.aop编程思想 1.面向切面,就是能够不动源码的情况下,从横切面切入新的代码功能. 2.实现原理是动态代理 动态代理的步骤 a.写生产厂家,实现接口,代理只能代理接口 b.动态代理类实现Invoc ...

  4. Spring AOP编程经验总结

    编程范式概览:面向过程,面向对象,函数式编程,事件驱动编程,面向切面等, AOP是什么? Spring AOP是采用面向切面编程的编程范式,而非编程语言,它只能解决特定问题,而非所有问题,它与OOP不 ...

  5. Spring AOP编程(一)-AOP介绍

    1. AOP介绍 l         在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...

  6. spring AOP编程--AspectJ注解方式

    1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...

  7. (转)Spring AOP编程原理、Demo

    转自: http://pandonix.iteye.com/blog/336873/ AOP原理: http://blog.csdn.net/moreevan/article/details/1197 ...

  8. spring AOP 编程--AspectJ注解方式 (4)

    1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...

  9. spring切面编程AOP 范例一

    参照网上的spring AOP编程实例进行配置,但是碰到了几个坑.这篇文章重点讲解一下我踩过的两个坑: 1.使用@Service自动装配的时候,基础扫描包配置要正确: 2.xml中切面配置中的exec ...

随机推荐

  1. FlexSlider插件的详细设置参数 http://www.woothemes.com/flexslider/ -----幻灯片插件

    $(window).load(function() { $('.flexslider').flexslider({ namespace: 'flex-', //控件的命名空间,会影响样式前缀 anim ...

  2. mysql概要(十四)索引

    1.索引是对数据库数据建立目录加快了查询速度.索引分为哈希索引和二叉树索引 (大数据量转移,如果表中带有大量字段索引,进行数据导入时,建议先去掉索引导入数据再统一加入索引,减少索引计算量) 2.索引原 ...

  3. Win7_关闭休眠文件hiberfil.sys

    1. C盘根目录下 hiberfil.sys 占用好几G空间,直接删 删不掉,也不推荐直接删. 2. 2.1.命令窗口中输入 powercfg -h off,即可关闭休眠功能,同时 Hiberfil. ...

  4. JS常用方法函数

    document.write("");为 输出语句    2.JS中的注释为//    3.传统的HTML文档顺序是:document->html->(head,bod ...

  5. (一)stm32之CMSIS标准、库目录、GPIO

    一.CMSIS标准 ST公司的stm32采用的是cortex-m3内核,内核是整个微处理器的CPU.该内核是ARM公司设计的一种处理器体系架构.内核与外设的关系就像PC上的CPU与硬盘.主板.内存等的 ...

  6. 动态替换fragment

    // [1]获取手机的宽和高 windommanager WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); in ...

  7. C++—复合类型

    内容概要: -创建和使用数组 -创建和使用C-风格字符串 -创建和使用string类字符串 -使用方法getline()和get()读取字符串 -混合输入字符串和数字 -创建和使用结构 -创建和使用共 ...

  8. 使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录

    使用连接(JOIN)来代替子查询(Sub-Queries) MySQL从 4.1开始支持SQL的子查询.这个技术可以使用SELECT语句来创建一个单列的查询结果,然后把这个结果作为过滤条件用在另一个查 ...

  9. bzoj3529(莫比乌斯反演+离线+树状数组)

    在你以为理解mobus的时候,苦苦想通过化简公式来降低复杂度时,这题又打了我一巴掌. 看来我并没有理解到acmicpc比赛的宗旨啊. 这么多次查询可以考虑离线操作,使用树状数组单点更新. /***** ...

  10. 转! java 中“==” 与“ .equals ”比较

    在java程序设计中,经常需要比较两个变量值是否相等.例如1.简单数据类型比较a = 10;b = 10;if(a == b){//写要执行的代码}2.引用数据类型比较ClassA a = new C ...