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. SAP接口编程 之 JCo3.0系列(05) : Exception Handling

    JCO3.0的Exception,常用的Exception如下: JCoException 继承自java.lang.Exception,是JCo3中Exception的基类. JCoRuntimeE ...

  2. pod JONSKit.h MBProgress.h 找不到头文件,怎么办?

    这时你看项目pod部分,多了JSONKit库.好了,第三方库就这么神奇的加进来. 头文件路径 那试试看使用JONSKit.h,在ViewController.m里引用下.找不到头文件,怎么办?还没设置 ...

  3. 淘宝封装的一款ui 非常不错

    好用的淘宝ui http://m.sui.taobao.org/demos/  手机端访问地址

  4. Http报头Accept与Content-Type的区别

    Http报头Accept与Content-Type的区别 1.Accept属于请求头, Content-Type属于实体头. Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http ...

  5. JavaScript的事件对象_事件流

    事件流事件流是描述的从页面接受事件的顺序,当几个都具有事件的元素层叠在一起的时候,那么你点击其中一个元素,并不是只有当前被点击的元素会触发事件,而层叠在你点击范围的所有元素都会触发事件.事件流包括两种 ...

  6. velocity基础教程--1.标准使用(zhuan)

    http://llying.iteye.com/blog/387253 **************************** velocity是一个非常好用的模板引擎 这里不对项目进行详细介绍,可 ...

  7. Hbase之更新单条数据

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  8. 【c++】读写txt

    #include<iostrea> #include<fstream> int main() { ofstream fout;// 创建一个ofstream对象 fout.op ...

  9. java 集合2(迭代器)

    迭代器方法:(把迭代器想象成抓娃娃机的爪子) hasNext()     问是否有元素可遍历,如果有元素可以遍历,返回true,否则返回false 工作原理:这一个迭代的过程是这样的,获取到迭代器时候 ...

  10. Unity5 新功能解析--物理渲染与standard shader

    Unity5 新功能解析--物理渲染与standard shader http://blog.csdn.net/leonwei/article/details/48395061 物理渲染是UNITY5 ...