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. iOS - OC NSDate 时间

    前言 NSDate @interface NSDate : NSObject <NSCopying, NSSecureCoding> NSDate 用来表示公历的 GMT 时间(格林威治时 ...

  2. PHP面向对象(OOP)编程入门教程————如何实例化对象?

    我们上面说过面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,既然我们类会声明了,下一步就是实例化对象了. 当定义好类后,我们使用new关键字来生成一个对象. $对象名称 = new 类名称 ...

  3. OCR识别-python版(一)

    需求:识别图片中的文字信息环境:windows系统 开发语言:python 使用工具类:1.pyocr 2.PIL 3.tesseract-ocr 步骤: 1.pyocr 网络通直接使用命令:pip ...

  4. Eclipse工作空间相关操作

    1.设置启动时是否弹出选择工作空间的提示框: 2.切换工作空间: 3.彻底删除eclipse不用的工作空间: 在eclipse的安装目录下:eclipse\configuration\.setting ...

  5. ps aux和ps ef的区别

    ps aux 是以BSD方式显示ps -ef 是以System V方式显示,该种方式比BSD方式显示的多一重要项--(具体哪项忘了 -_- ) ps aux的输出: USER PID %CPU %ME ...

  6. 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小

    原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...

  7. Asp.Net_Web身份验证

    百度一下”asp.net身份认证“,你会得到很多相关的资料,这些资料通常上来就会介绍诸如”Form认证“”Windows认证“等内容,而没有给出一个完整的流程.初学者对此往往一头雾水,我也曾经被坑过很 ...

  8. SecureCRT设置

    SecureCRT设置 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39890569 本文主要介绍SecureCRT的使用方法和技巧. ...

  9. cpu缓存与多线程

    一.cpu缓存结构 CPU速度远高于内存(即如果只考虑CPU和内存因素,程序的性能常常受到内存访问速度的限制,内存访问和运行),为了协调CPU和内存在速度上的差异,在CPU中增加了高速缓存.和计算机存 ...

  10. Weka 3: Data Mining Software in Java

    官方网站: Weka 3: Data Mining Software in Java 相关使用方法博客 WEKA使用教程(经典教程转载) (实例数据:bank-data.csv) Weka初步一.二. ...