Spring AOP編程

來源:互聯網
上載者:User
一篇關於使用Spring AOP的教程,將晦澀難懂的Spring AOP講解得簡單清晰。原文參見這裡。

        AOP正在成為軟體開發的下一個聖杯。使用AOP,你可以將處理aspect的代碼注入主程式,通常主程式的主要目的並不在於處理這些aspect。AOP可以防止代碼混亂。       為了理解AOP如何做到這點,考慮一下記日誌的工作。日誌本身不太可能是你開發的主程式的主要任務。如果能將“不可見的”、通用的日誌代碼注入主程式中,那該多好啊。AOP可以協助你做到。       Spring framework是很有前途的AOP技術。作為一種非侵略性的,輕型的AOP framework,你無需使用先行編譯器或其他的元標籤,便可以在Java程式中使用它。這意味著Team Dev裡只需一人要對付AOP framework,其他人還是象往常一樣編程。       AOP是很多直覺難以理解的術語的根源。幸運的是,你只要理解三個概念,就可以編寫AOP模組。這三個概念是:advice,pointcut和advisor。advice是你想向別的程式內部不同的地方注入的代碼。pointcut定義了需要注入advice的位置,通常是某個特定的類的一個public方法。advisor是pointcut和advice的裝配器,是將advice注入主程式中預定義位置的代碼。        既然我們知道了需要使用advisor向主要代碼中注入“不可見的”advice,讓我們實現一個Spring AOP的例子。在這個例子中,我們將實現一個before advice,這意味著advice的代碼在被調用的public方法開始前被執行。以下是這個before advice的實現代碼: package com.company.springaop.test;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class TestBeforeAdvice implements MethodBeforeAdvice {

  public void before(Method m, Object[] args, Object target)
  throws Throwable {
    System.out.println("Hello world! (by "
        + this.getClass().getName()
        + ")");
  }
} 介面MethodBeforeAdvice只有一個方法before需要實現,它定義了advice的實現。before方法共用三個參數,它們提供了相當豐富的資訊。參數Method m是advice開始後執行的方法。方法名稱可以用作判斷是否執行代碼的條件。Object[] args是傳給被調用的public方法的參數數組。當需要記日誌時,參數args和被執行方法的名稱,都是非常有用的資訊。你也可以改變傳給m的參數,但要小心使用這個功能;編寫最初主程式的程式員並不知道主程式可能會和傳入參數的發生衝突。Object target是執行方法m對象的引用。              在下面的BeanImpl類中,每個public方法調用前,都會執行advice:

package com.company.springaop.test;public class BeanImpl implements Bean {  public void theMethod() {    System.out.println(this.getClass().getName()        + "." + new Exception().getStackTrace()[0].getMethodName()        + "()"        + " says HELLO!");  }}
類BeanImpl實現了下面的介面Bean:
package com.company.springaop.test;public interface Bean {  public void theMethod();}
雖然不是必須使用介面,但面向介面而不是面向實現編程是良好的編程實踐,Spring也鼓勵這樣做。
pointcut和advice通過設定檔來實現,因此,接下來你只需編寫主方法的Java代碼:
package com.company.springaop.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class Main {  public static void main(String[] args) {    //Read the configuration file    ApplicationContext ctx        = new FileSystemXmlApplicationContext("springconfig.xml");    //Instantiate an object    Bean x = (Bean) ctx.getBean("bean");    //Execute the public method of the bean (the test)    x.theMethod();  }}
       我們從讀入和處理設定檔開始,接下來馬上要建立它。這個設定檔將作為粘合程式不同部分的“膠水”。讀入和處理設定檔後,我們會得到一個建立工廠ctx。任何一個Spring管理的對象都必須通過這個工廠來建立。對象通過工廠建立後便可正常使用。        僅僅用設定檔便可把程式的每一部分組裝起來。<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <!--CONFIG-->
  <bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>com.company.springaop.test.Bean</value>
    </property>
    <property name="target">
      <ref local="beanTarget"/>
    </property>
    <property name="interceptorNames">
      <list>
        <value>theAdvisor</value>
      </list>
    </property>
  </bean>

  <!--CLASS-->
  <bean id="beanTarget" class="com.company.springaop.test.BeanImpl"/>

  <!--ADVISOR-->
  <!--Note: An advisor assembles pointcut and advice-->
  <bean id="theAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="theBeforeAdvice"/>
    </property>
    <property name="pattern">
      <value>com\.company\.springaop\.test\.Bean\.theMethod</value>
    </property>
  </bean>

  <!--ADVICE-->
  <bean id="theBeforeAdvice" class="com.company.springaop.test.TestBeforeAdvice"/>
</beans>       四個bean定義的次序並不重要。我們現在有了一個advice,一個包含了Regexpointcut的advisor,一個主程式類和一個配置好的介面,通過工廠ctx,這個介面返回自己本身實現的一個引用。      BeanImpl和TestBeforeAdvice都是直接配置。我們用一個唯一的ID建立一個bean元素,並指定了一個實作類別。這就是全部的工作。      advisor通過Spring framework提供的一個RegexMethodPointcutAdvisor類來實現。我們用advisor的一個屬性來指定它所需的advice-bean。第二個屬性則用Regex定義了pointcut,確保良好的效能和易讀性。      最後配置的是bean,它可以通過一個工廠來建立。bean的定義看起來比實際上要複雜。bean是ProxyFactoryBean的一個實現,它是Spring framework的一部分。這個bean的行為通過一下的三個屬性來定義:

  • 屬性proxyInterface定義了介面類。
  • 屬性target指向本地配置的一個bean,這個bean返回一個介面的實現。
  • 屬性interceptorNames是唯一允許定義一個值列表的屬性。這個列表包含所有需要在beanTarget上執行的advisor。注意,advisor列表的次序是非常重要的。
Spring工具       雖然你可以手工修改Ant構建指令碼,但使用SpringUI(譯註:SpringUI現在是Spring framework的一部分,並改名為spring-ide),使用Spring AOP變得很簡單,只要點點滑鼠即可。你可以把SpringUI安裝成Eclipse的一個plug-in。然後,你只需在你的project上右擊滑鼠,並選擇“add Spring Project Nature”。在project屬性中,你可以在“Spring Project”下添加Spring設定檔。在編譯前把下面的類庫加入project:aopalliance.jar,commons-logging.jar,jakarta-oro-2.0.7.jar和spring.jar。運行程式時你會看到下面的資訊:
... (logging information)Hello world! (by com.company.springaop.test.TestBeforeAdvice)com.company.springaop.test.BeanImpl.theMethod() says HELLO!
優點和缺點        Spring比起其他的framework更有優勢,因為除了AOP以外,它提供了更多別的功能。作為一個輕型framework,它在J2EE不同的部分都可以發揮作用。因此,即使不想使用Spring AOP,你可能還是想使用Spring。另一個優點是,Spring並不要求Team Dev所有的人員都會用它。學習Spring應該從Spring reference的第一頁開始。讀了本文後,你應該可以更好地理解Spring reference了。Spring唯一的缺點是缺乏更多的文檔,但它的mailing list是個很好的補充,而且會不斷地出現更多的文檔。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.