作为这个介绍spring框架中的面向方面编程(aspect-oriented programming,aop)的系列的第一部分,本文介绍了使您可以使用spring中的面向方面特性进行快速开发的基础知识。使用跟踪和记录方面(面向方面领域的helloworld)作为例子,本文展示了如何使用spring框架所独有的特性来声明切入点和通知以便应用方面。本系列的第二部分将更深入地介绍如何运用spring中的所有通知类型和切入点来实现更实用的方面和面向方面设计模式。对于aop的更一般性的介绍,请查看onjava站点上graham o'regan的文章,“introduction to aspect-oriented programming”。
本文的目的不是要介绍构成模块化j2ee系统――即spring框架――的所有重要元素,我们将只把注意力放在spring所提供的aop功能上。由于spring的模块化设计方法,我们可以只使用该框架的aop元素,而无需对构成spring框架的其他模块做太多考虑。
在aop方面,spring提供了什么?
“它的目标不是提供最完善的aop实现(虽然spring aop非常强大);而是要提供aop实现与spring ioc的紧密集成,以便帮助解决企业应用中的常见问题。”
spring framework参考文档
为了实现这个目标,spring框架目前支持一组aop概念,从切入点到通知。本文将展示如何使用spring框架中所实现的如下aop概念:
通知(advice):如何将before通知、afterreturning通知和afterthrowing通知声明为bean。
切入点(pointcut):如何声明静态切入点逻辑以将xml spring bean configuration文件中的所有内容联系在一起。
advisor:关联切入点定义与通知bean的方式。
设置场景:一个简单的例子应用程序
“一般而言,spring并不是预描述的。虽然使用好的实践非常容易,但是它避免强制推行一种特定的方法。”
spring framework参考文档
要试用spring框架的aop功能,首先我们要创建一个简单的java应用程序。ibusinesslogic接口和businesslogic类为spring框架中的bean提供了简易构件块。虽然该接口对于我们的简单应用程序逻辑来说不是必需的,但是它是spring框架所推荐的良好实践。
| public interface ibusinesslogic { public void foo(); } public class businesslogic implements ibusinesslogic { public void foo() { system.out.println("inside businesslogic.foo()"); } } |
可以编写mainapplication类,借此练习businesslogic bean的公有方法。
| import org.springframework.context.applicationcontext; import org.springframework.context.support.filesystemxmlapplicationcontext; public class mainapplication { public static void main(string [] args) { // read the configuration file applicationcontext ctx =new filesystemxmlapplicationcontext("springconfig.xml"); //instantiate an object ibusinesslogic testobject =(ibusinesslogic) ctx.getbean("businesslogicbean"); // execute the public // method of the bean testobject.foo(); } } |
在businesslogic类及其关联接口中没有什么需要注意的。但是,mainapplication类初始化businesslogic对象的方式很有意思。通过使用ctx.getbean("businesslogicbean")调用,mainapplication将加载和管理businesslogic类的bean实例的任务转交给了spring框架。
允许spring控制businesslogic bean的初始化,这使得spring运行时有机会在bean被返回给应用程序之前执行j2ee系统所需的所有与bean相关的管理任务。然后spring运行时配置可以决定对bean应用哪些任务和模块。该配置信息由一个xml文件提供,类似于下面所示的:
| <?xml version="1.0" encoding="utf-8"?> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- bean configuration --> <bean id="businesslogicbean" class="org.springframework.aop.framework.proxyfactorybean"> <property name="proxyinterfaces"> <value>ibusinesslogic</value> </property> <property name="target"> <ref local="beantarget"/> </property> </bean> <!-- bean classes --> <bean id="beantarget" class="businesslogic"/> </beans> </beans> |
该配置文件,即springconfig.xml,指定要加载一个接口与ibusinesslogic相匹配的bean。该bean随后被关联到businesslogic实现类。看起来好像是费了很大力气只为了加载一个简单的bean并调用一个方法,但是您要知道,这个配置文件只是使spring框架可以透明地对应用程序应用其组件的众多特性的一个体现。
图1显示了基本的顺序图:mainapplication原样执行,没有应用方面。

图1.没有对businesslogic bean应用方面时的顺序图 应用方法跟踪(method tracing)方面
可能最基本的方面就是方法跟踪方面了。这可能是您找得到的最简单的方面了,因此它是研究新的aop实现的一个很好的起点。
方法跟踪方面在一个目标应用程序内捕获对所跟踪的方法的调用以及方法的返回值,并以某种方式显示这种信息。在aop中,通知的before和after类型用于捕获这些类型的联结点,因为这两种通知可以在方法调用联结点之前或之后触发。使用spring框架,方法跟踪方面的before通知是在tracingbeforeadvice类中声明的。
| import java.lang.reflect.method; import org.springframework.aop. methodbeforeadvice; public class tracingbeforeadvice implements methodbeforeadvice { public void before(method m,object[] args,object target) throws throwable { system.out.println("hello world! (by " +this.getclass().getname() +")"); } } |
类似地,after通知可以在tracingafteradvice类中声明。
| import java.lang.reflect.method; import org.springframework.aop.afterreturningadvice; public class tracingafteradvice implements afterreturningadvice { public void afterreturning(object object,method m,object[] args,object target) throws throwable { system.out.println("hello world! (by " +this.getclass().getname() +")"); } } |
这两个类都通过实现spring框架的适当通知接口而表示了特定的通知。每种类型的通知都指定实现before(..)或afterreturning(..)方法,以便使spring运行时可以告诉通知适当的联结点会在何时出现。值得注意的是,tracingafteradvice实际上是从afterreturningadvice扩展而来的,表示只有在联结点在无异常的情况下获得返回值时才运行通知。
为了将通知与应用程序中的适当联结点关联起来,必须对springconfig.xml进行一些修改。
| <?xml version="1.0" encoding="utf-8"?> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- bean configuration --> <bean id="businesslogicbean" class="org.springframework.aop.framework.proxyfactorybean"> <property name="proxyinterfaces"> <value>ibusinesslogic</value> </property> <property name="target"> <ref local="beantarget"/> </property> <property name="interceptornames"> <list> <value>thetracingbeforeadvisor</value> <value>thetracingafteradvisor</value> </list> </property> </bean> <!-- bean classes --> <bean id="beantarget" class="businesslogic"/> <!-- advisor pointcut definition for before advice --> <bean id="thetracingbeforeadvisor" class="org.springframework.aop.support.regexpmethodpointcutadvisor"> <property name="advice"> <ref local="thetracingbeforeadvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!-- advisor pointcut definition for after advice --> <bean id="thetracingafteradvisor" class="org.springframework.aop.support.regexpmethodpointcutadvisor"> <property name="advice"> <ref local="thetracingafteradvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!-- advice classes --> <bean id="thetracingbeforeadvice" class="tracingbeforeadvice"/> <bean id="thetracingafteradvice" class="tracingafteradvice"/> </beans> |
thetracingbeforeadvisor和thetracingafteradvisor advisor被添加到前面所声明的businesslogicbean。每个advisor都可能截获所有bean所关联到的联结点。advisor本身就是bean,而它唯一的作用就是将切入点定义与通知bean关联起来。本例中的切入点定义是在静态对象层次结构中指定相关联结点的正则表达式。
因为本例中使用了org.springframework.aop.support.regexpmethodpointcutadvisor切入点advisor,切入点逻辑是使用正则表达式指定的。正则表达式用于识别公有接口对ibusinesslogici接口的联结点。下面是一些可以用来指定ibusinesslogic接口上的不同联结点集合的正则表达式例子:
springconfig.xml文件中最后的bean声明指定实现通知bean的类。
既然已经指定了跟踪方面的正确配置,那么下一次执行mainapplication时,这些方面就会在初始化过程中被编织进去,而businesslogic bean中的所有方法都将被跟踪,如图2所示。

图2. 方法跟踪方面应用到businesslogic bean之后的顺序图 方面的重用
可以对方法跟踪方面进行扩展,提供一个稍微复杂的记录(logging)方面。记录方面提供了一个很不错的重用例子,因为记录方面所需的许多特性都已经包含在方法跟踪方面中了。
在本例中,记录方面扩展了方法跟踪方面,以便显示附加的与(在应用程序的执行过程中)所引发的异常有关的信息。
要完全使用记录方面,需要对应用程序做一些更改。businesslogicexception异常类提供了一个可以由ibusinesslogicinterface接口和businesslogic实现类新增的void bar()方法引发的异常。
| public class businesslogicexception extends exception {} public interface ibusinesslogic { public void foo(); public void bar() throws businesslogicexception; } public class businesslogic implements ibusinesslogic { public void foo() { system.out.println("inside businesslogic.foo()"); } public void bar() throws businesslogicexception { system.out.println("inside businesslogic.bar()"); throw new businesslogicexception(); } } |
mainapplication类现在将对void bar()方法进行一次额外的调用,并处理选中的、可能由该方法引发的异常。
| import org.springframeworkcontext.applicationcontext; import org.springframework.context.support.filesystemxmlapplicationcontext; public class mainapplication { public static void main(string [] args) { // read the configuration file applicationcontext ctx = new filesystemxmlapplicationcontext( "springconfig.xml"); //instantiate an object ibusinesslogic testobject =(ibusinesslogic) ctx.getbean("businesslogicbean"); //execute the public methods of the bean testobject.foo(); try { testobject.bar(); } catch(businesslogicexception ble) { system.out.println( "caught businesslogicexception"); } } } |
来自方法跟踪方面的tracingbeforeadvice和tracingafteradvice通知可以整体重用。loggingthrowsadvice类为新的异常记录提供了通知。
| import org.springframework.aop.throwsadvice; import java.lang.reflect.method; public class loggingthrowsadvice implements throwsadvice { public void afterthrowing(method method,object[] args,object target,throwable subclass) { system.out.println("logging that a " +subclass +"exception was thrown."); } } |
应用记录方面的最后一步是修改springconfig.xml配置文件,使其包含新添加的loggingthrowsadvice通知。
图3显示了运行mainapplication并使用spring框架应用了记录方面的uml顺序图。
(图片较大,请拉动滚动条观看)
图3. 记录方面应用到businesslogic bean之后的顺序图
此处的记录方面清楚地说明了如何重用现有方面以及如何在spring框架中使用通知的throws形式。通过为before和after通知声明新的通知来重写现有的方法跟踪方面实现,可以实现更复杂的记录方面,记录到更复杂的记录框架,比如log4j。
结束语
本文展示了使用spring框架中的基本aop结构所应用的一些简单方面。在本系列的下一篇文章中,我们将介绍一些更实用的方面,探讨方面的生命周期,使用spring框架的around通知,并使用spring来应用aop模式。
闽公网安备 35060202000074号