欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Spring Framework中的AOP編程之入門(mén)篇 2

Spring Framework中的AOP編程之入門(mén)篇

2005-12-26 15:30 作者: Russell Miles 出處: bea 責任編輯:方舟
  應用方法跟蹤(Method Tracing)方面

  可能最基本的方面就是方法跟蹤方面了。這可能是您找得到的最簡(jiǎn)單的方面了,因此它是研究新的AOP實(shí)現的一個(gè)很好的起點(diǎn)。

  方法跟蹤方面在一個(gè)目標應用程序內捕獲對所跟蹤的方法的調用以及方法的返回值,并以某種方式顯示這種信息。在A(yíng)OP中,通知的before和after類(lèi)型用于捕獲這些類(lèi)型的聯(lián)結點(diǎn),因為這兩種通知可以在方法調用聯(lián)結點(diǎn)之前或之后觸發(fā)。使用Spring框架,方法跟蹤方面的before通知是在TracingBeforeAdvice類(lèi)中聲明的。

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() + ")");
 }
}

  類(lèi)似地,after通知可以在TracingAfterAdvice類(lèi)中聲明。

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() + ")");
 }
}

  這兩個(gè)類(lèi)都通過(guò)實(shí)現Spring框架的適當通知接口而表示了特定的通知。每種類(lèi)型的通知都指定實(shí)現before(..)或afterReturning(..)方法,以便使Spring運行時(shí)可以告訴通知適當的聯(lián)結點(diǎn)會(huì )在何時(shí)出現。值得注意的是,TracingAfterAdvice實(shí)際上是從AfterReturningAdvice擴展而來(lái)的,表示只有在聯(lián)結點(diǎn)在無(wú)異常的情況下獲得返回值時(shí)才運行通知。

  為了將通知與應用程序中的適當聯(lián)結點(diǎn)關(guān)聯(lián)起來(lái),必須對springconfig.xml進(jìn)行一些修改。

<?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。每個(gè)advisor都可能截獲所有bean所關(guān)聯(lián)到的聯(lián)結點(diǎn)。Advisor本身就是bean,而它唯一的作用就是將切入點(diǎn)定義與通知bean關(guān)聯(lián)起來(lái)。本例中的切入點(diǎn)定義是在靜態(tài)對象層次結構中指定相關(guān)聯(lián)結點(diǎn)的正則表達式。

  因為本例中使用了org.springframework.aop.support.RegexpMethodPointcutAdvisor切入點(diǎn)advisor,切入點(diǎn)邏輯是使用正則表達式指定的。正則表達式用于識別公有接口對IbusinessLogici接口的聯(lián)結點(diǎn)。下面是一些可以用來(lái)指定IBusinessLogic接口上的不同聯(lián)結點(diǎn)集合的正則表達式例子:

<value>.*</value>:該表達式選擇advisor所關(guān)聯(lián)到的一個(gè)或多個(gè)bean上的所有聯(lián)結點(diǎn)。
<value>./IBusinessLogic/.foo</value>:該表達式只選擇IbusinessLogic接口上的foo()方法的聯(lián)結點(diǎn)。如果是advisor所關(guān)聯(lián)到的bean,則該表達式只選擇IBusinessLogic接口上的聯(lián)結點(diǎn)。

  springconfig.xml文件中最后的bean聲明指定實(shí)現通知bean的類(lèi)。

  既然已經(jīng)指定了跟蹤方面的正確配置,那么下一次執行MainApplication時(shí),這些方面就會(huì )在初始化過(guò)程中被編織進(jìn)去,而B(niǎo)usinessLogic bean中的所有方法都將被跟蹤,如圖2所示。



圖2. 方法跟蹤方面應用到BusinessLogic bean之后的順序圖(單擊圖像查看大圖)

  方法跟蹤方面和例子應用程序的源代碼可在本文末尾的參考資料小節進(jìn)行下載。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Spring Framework中的面向方面編程(AOP)
SpringFramework(4)
[讀書(shū)筆記]Spring AOP Review - Robin‘s Java World ...
spring學(xué)習總結
使用Atomikos Transactions Essentials實(shí)現多數據源JTA分布式事務(wù)
使用spring+atomikos+ibatis實(shí)現聲明式DB2多數據庫全局分布式事務(wù)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久