作為這個介紹spring框架中的面向方面編程(aspect-oriented programming,aop)的系列的第一部分,本文介紹了使您可以使用spring中的面向方面特性進行快速開發的基礎知識。使用跟蹤和記錄方面(面向方面領域的helloworld)作為例子,本文展示了如何使用spring框架所獨有的特性來聲明切入點和通知以便應用方面。本系列的第二部分將更深入地介紹如何運用spring中的所有通知類型和切入點來實現更實用的方面和面向方面設計模式。
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">
<property name="proxyinterfaces">
<value>ibusinesslogic</value>
</property>
<property name="target">
<ref local="beantarget"/>
</property>
</bean>
<!-- bean classes -->
<bean id="beantarget"
class="businesslogic"/>
</beans>

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>
<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>
<value>.*</value>:該表達式選擇advisor所關聯到的一個或多個bean上的所有聯結點。 
<value>./ibusinesslogic/.foo</value>:該表達式只選擇ibusinesslogic接口上的foo()方法的聯結點。如果是advisor所關聯到的bean,則該表達式只選擇ibusinesslogic接口上的聯結點。 

  方法跟蹤方面和例子應用程序的源代碼可在本文末尾的參考資料小節進行下載。
  方面的重用
  可以對方法跟蹤方面進行擴展,提供一個稍微復雜的記錄(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.");
 } 
}

新聞熱點
疑難解答