public interface ibusinesslogic
{
public void foo();
}
public class businesslogic
implements ibusinesslogic
{
public void foo()
{
system.out.println("inside businesslogic.foo()");
}
}
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();
}
}
<?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">
?。紁roperty name="proxyinterfaces">
<value>ibusinesslogic</value>
</property>
?。紁roperty name="target">
?。紃ef local="beantarget"/>
?。?property>
?。?bean>
<!-- bean classes -->
?。糱ean id="beantarget" class="businesslogic"/>
</beans>
</beans>
該配置文件,即springconfig.xml,指定要加載一個接口與ibusinesslogic相匹配的bean。該bean隨后被關聯到businesslogic實現類??雌饋砗孟袷琴M了很大力氣只為了加載一個簡單的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() +")");
}
}
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() +")");
}
}
<?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>
?。紁roperty name="target">
?。紃ef local="beantarget"/>
</property>
?。紁roperty name="interceptornames">
?。糽ist>
?。紇alue>thetracingbeforeadvisor</value>
?。紇alue>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">
?。紃ef local="thetracingbeforeadvice"/>
?。?property>
?。紁roperty name="pattern">
?。紇alue>.*</value>
?。?property>
</bean>
<!-- advisor pointcut definition for after advice -->
<bean id="thetracingafteradvisor" class="org.springframework.aop.support.regexpmethodpointcutadvisor">
?。紁roperty name="advice">
<ref local="thetracingafteradvice"/>
</property>
?。紁roperty name="pattern">
?。紇alue>.*</value>
?。?property>
</bean>
<!-- advice classes -->
<bean id="thetracingbeforeadvice" class="tracingbeforeadvice"/>
<bean id="thetracingafteradvice" class="tracingafteradvice"/>
</beans>

方面的重用
可以對方法跟蹤方面進行擴展,提供一個稍微復雜的記錄(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();
}
}
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");
}
}
}
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.");
}
}

此處的記錄方面清楚地說明了如何重用現有方面以及如何在spring框架中使用通知的throws形式。通過為before和after通知聲明新的通知來重寫現有的方法跟蹤方面實現,可以實現更復雜的記錄方面,記錄到更復雜的記錄框架,比如log4j。
結束語
本文展示了使用spring框架中的基本aop結構所應用的一些簡單方面。在本系列的下一篇文章中,我們將介紹一些更實用的方面,探討方面的生命周期,使用spring框架的around通知,并使用spring來應用aop模式。
新聞熱點
疑難解答