Spring中基于aop命名空间的AOP 一(一点准备工作和一个例子)

(2008年08月23日)发表于JavaEye博客

作者: cmzy  链接: http://cmzy.javaeye.com/blog/231877  发表时间: 2008年08月23日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

 

(残梦追月原创,转载请注明)

本文地址: http://www.blogjava.net/cmzy/archive/2008/08/23/223870.html

    在某些时候,我们工程中使用的JDK 不一定就是1.5 以上,也就是说可能不支持Annotation 注解,这时自然也就不能使用@AspectJ 注解驱动的AOP 了,那么如果我们仍然想使用AspectJ 灵活的切入点表达式,那么该如何呢?Spring 为我们提供了基于xml schematic 的aop 命名空间,它的使用方式和@AspectJ 注解类似,不同的是配置信息从注解中转移到了Spring 配置文件中。在这里,我们将详细介绍如何使用Spring 提供的<aop:config/> 标签来配置Spring AOP 。


1 、一点准备工作和一个例子

    使用<aop:config/> 标签,需要给Spring 配置文件中引入基于xml schema 的Spring AOP 命名空间。完成后的Spring 配置文件如下(在该节,所有例程的配置文件中添加了Spring AOP 命名空间,除非特殊情况外,为了节约空间,这部分将在给出的代码中省略),粗体内容即为我们需要添加的内容:

    关于aop命名空间的标签,我们前面使用过的有<aop:aspectj-autoproxy/>,在这一节,我们将以<aop:config/>标签作为重点。事实上,我们在这一节介绍的所有标签都是该标签的子标签。


   下面有一个例程来直观的展示如何使用<aop:config/>标签来配置Spring AOP(完整代码见例程4.15)。在例子中,我们使用<aop:config/>配置一个切面并拦截目标对象Peoples的SayHello()方法,在它执行前输出提示信息。
首先创建工程AOP_Test4.15,添加Spring IoC和Spring AOP库后,创建aop.test包,新建目标类People,代码如下:

    修改Spring xml配置文件,将该类注册为一个受管Bean:

    创建含有main()方法的测试类TestMain,从Spring IoC容器中获取Peoples对象,并调用其SayHello()方法,代码如下:

   创建MyAspect类,添加一个beforeAdvice()方法作为前置通知方法,代码如下:

    修改xml配置文件,为其添加aop命名空间,并把MyAspect注册为一个受管Bean,作为我们下面定义切面的backing bean。代码如下:

    运行主类,输出如下:

例程4.15输出结果

例程4.15输出结果


本文的讨论也很精彩,浏览讨论>>


JavaEye推荐



阅读全文...
本站相关内容:

Spring AOP Advice types

Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception). After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception

Spring AOP 起步

其实我的个人表达能力不是很好……估计小时候语文没学好~也可能是aop这个概念本身意会的程度很大吧!呵呵寒暄一下! 正式进入主题:AOP 即 Aspect Oriented Programming 的缩写,中文译为"面向切面编程"。本篇没那么学术化,只是为了快速入门了解真实的使用方式!我们不用那么早去想这个名词的意义,fellow me,用真实的代码透析AOP的含义。 不知各位有没有项目开发的经验,如果有的话你应该可以清楚地了解到我们的代码中常常充斥着大量的日志记录代码,我们用log4j等日志记录工具一段一段地记录程序运行的信息。也许有个别是十分特殊的,但是似乎大部分都是例行公事吧!或者在丢出exception的时候捕获其message然后记入日志对吗?这样的代码难道不觉得碍眼?修改起来是不是也很麻烦?要是我们换了一个日志记录工具怎么办?呵呵,别吓到了,没这么严重啦,这些都是极端情况,平时我们还是很开心地写着这些东西。但是今天要说到的这个aop可以将一些特别的操作提取出来,作为我们的"通知-advice",在运行时加载到对象中。就是说,我们可以在代码中不写日志记录段,但是

Spring AOP - Designating Pointcuts

Pointcut designators (PCD) allow flexible constraints on AOP pointcut match criteria for pointcut expressions. Here is a list of the AspectJ PCD’s which Spring AOP supports which limit the matching of join points to - execution - the primary pointcut designator you will use when working with Spring AOP within - within certain types (simply the execution of a method declared within a matching type when using Spring AOP) this - where the bean reference (Spring AOP proxy) is an instance of the

Spring AOP Docs

If this were a book the pages of mine would be well worn: http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-introduction

Spring AOP 起步

其实我的个人表达能力不是很好……估计小时候语文没学好~也可能是aop这个概念本身意会的程度很大吧!呵呵寒暄一下! 正式进入主题:AOP 即 Aspect Oriented Programming 的缩写,中文译为”面向切面编程”。本篇没那么学术化,只是为了快速入门了解真实的使用方式!我们不用那么早去想这个名词的意义,fellow me,用真实的代码透析AOP的含义。 不知各位有没有项目开发的经验,如果有的话你应该可以清楚地了解到我们的代码中常常充斥着大量的日志记录代码,我们用log4j等日志记录工具一段一段地记录程序运行的信息。也许有个别是十分特殊的,但是似乎大部分都是例行公事吧!或者在丢出exception的时候捕获其message然后记入日志对吗?这样的代码难道不觉得碍眼?修改起来是不是也很麻烦?要是我们换了一个日志记录工具怎么办?呵呵,别吓到了,没这么严重啦,这些都是极端情况,平时我们还是很开心地写着这些东西。但是今天要说到的这个aop可以将一些特别的操作提取出来,作为我们的”通知-advice”,在运行时加载到对象中。就是说,我们可以在代码中不写日志记录段,但是在运行时加

JSF and Spring AOP

Preface: Firstly, the point of this post is to illustrate a work around for integrating aspects of the Spring and JSF frameworks; namely how to integrate Spring's AOP aspects into JSF managed beans. This post also provides a brief background building up the concepts and problems addressed later in this post. Note that the workaround provided in this post is NOT ideal in that it can become somewhat of a nuisance to maintain; but, it does offer a way around some common problems. Background: If y

互联网相关内容:
Spring AOP Advice types (2007年10月18日)
Spring AOP 起步 (2007年12月07日)
Spring AOP - Designating Pointcuts (2008年01月10日)
Spring AOP Docs (2008年01月13日)
Spring AOP 起步 (2008年01月13日)
JSF and Spring AOP (2008年02月02日)
Spring + DWR namespaces + AOP (2008年02月16日)
DWR, Spring and AOP (2008年02月20日)
DWR, Spring and AOP (2008年02月20日)
DWR, Spring and AOP (2008年02月20日)