您可以在这里快速查找:


 
您的位置: 编程学习 > java教程 > 200511
文章分类

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02

Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

VB编程
2006: 02

Asp编程
2005: 11 12
2006: 01 02

C++/VC
2005: 10 11 12
2006: 01 02

Delphi
2005: 12
2006: 01 02

其它

 本文章适合所有读者

JBoss-IDE AOP 簡介 (翻譯)

patrickip

JBoss-IDE AOP 簡介 (翻譯)

 

1: 安裝

JBoss-IDE AOP 會包含在 JBoss-IDE 中, 可以在安裝 JBoss-IDE 時一起選 AOP 這個選項. 而安裝 JBoss-IDE 可參考 http://dev.csdn.net/article/25/25104.shtm. 如只是想單獨安裝 JBoss-IDE AOP, 在安裝JBoss-IDE 過程中不要選JBoss-IDE, 只選 ‘JBoss-IDE AOP Standalone’ 這項.

 

本篇會介紹 JBoss-IDE 裏的 AOP 應用, 所以建議連同 JBoss-IDE 一起安裝.

 

2: 簡介

這部份會教導如何在 eclipse 的 JBoss-IDE 裏創建一個簡單的 AOP 項目, 首先你要對 java, AOP 以及 eclipse 有一定的認識才可繼續.

 

2.1: 創建項目

在 eclipse 的主目錄, 選 ´File Menu´ -> ´New´ -> ´Project...´

在 ´JBossAOP´ 選項中雙擊 ´JBoss AOP Project´

在 ´Project Name´ 裏填入 ´HelloAOP´

其他設定可全省缺, 按 ´Finish´

 

2.2: 創建類

接著是創建一個正常的 java 類, 在 eclipse 的項目樹中擴展 ´HelloAOP´ 右擊 ´src´ -> ´New´ -> ´Class´

´Name´ 填入 ´HelloAOP´ 再按 ´Finish´

 

在 ´HelloAOP´ 裏修改成以下的代碼:

 

public class HelloAOP {

 

            public void callMe () {

                        System.out.println("AOP!");

            }

           

            public static void main (String args[])           {

                        new HelloAOP().callMe();

            }

}

 

2.3: 創建 Interceptor

接著是新增一個 Interceptor 類

在 eclipse 的項目樹中擴展 ´HelloAOP´ 右擊 ´src´ -> ´New´ -> ´Class´

´Name´ 填入 ´HelloAOPInterceptor´, ´Interface´ 選 ´Interceptor´ (org.jboss.aop.advice.Interceptor) 再按 ´Finish´

 

在 ´HelloAOPInterceptor´ 裏修改成以下的代碼:

 

import org.jboss.aop.advice.Interceptor;

import org.jboss.aop.joinpoint.Invocation;

  

public class HelloAOPInterceptor implements Interceptor {

  

            public String getName() {

                        return "HelloAOPInterceptor";

            }

  

        //We renamed the arg0 parameter to invocation

            public Object invoke(Invocation invocation) throws Throwable {

                        System.out.print("Hello, ");

                //Here we invoke the next in the chain

                        return invocation.invokeNext();

            }

}

 

2.4: Interceptor 運用

現在我們想在 HelloAOP 的 callme() 方法上運用 Interceptor, 首先在 eclipse 裏要在編輯 ´HelloAOP.java´ 的視窗

在下方的 ´Outline´ 視窗會看到 callme() 方法. (如看不到可在主目錄欄中選 ´Window´ -> ´Show View´ -> ´Outline´)

右擊這個方法, 按 ´JBoss AOP´ -> ´Apply Interceptor(s)´

然後會出現一列 Interceptors, 選 ´HelloAOPInterceptor´ 再按 ´Finish´

成功後會自動更改項目中的 ´jboss-aop.xml´ 檔案

 

2.5: 執行

首先要建立一個執行程序, 在上方主目錄欄中按 ´Run...´

在彈出的視窗左手邊雙擊 ´JBoss AOP Application´, 接著會出現新的執行設定 ´Hello AOP´, 按下它然後再按 ´Run´

 

跟著在下方視窗中 ´console´ 會出現 ´Hello AOP!´, 而 ´Hello´ 是由 interceptor 加入.

 

3: 特色

當在真正做一些大型項目時, 會涉及不同程式員所負責的不同代碼, 這時會出現不知道那裏會應用了 aspect 等問題,

JBoss-IDE/AOP 用了一些策略去令程式員留意這些地方.

 

3.1: 建議標籤

標籤在 eclipse 中是出現在編輯視窗左手方的一個很小的圖案, 相信大部份用開 eclispe 的程式員都會知道.

AOP IDE 亦會在有 interceptor 的地方顯示一個標籤. 只要在已標籤的代碼按 ´CTRL + 1´, 所有在項目中的 Interceptors 裏的方法及屬性會出現.

 

這樣令找尋 Interceptors 時會非常方便.

 

3.2: 建議標籤視窗

建議標籤視窗可令程式員在每一個類內看到所有在這個類的 Interceptors.

在主目錄欄中選 ´Window´ -> ´Show View´ -> ´Other...´

在彈出的視窗中會看到 ´JBoss AOP´ 按 ´+´ 擴展後選 ´Advised Members´

 

完成後會在 eclipse 下方中顯示建議標籤視窗.

 

3.3: Aspect 管理員視窗

The Aspect Manager View is a graphical representation of the AOP descriptor file (jboss-aop.xml).

It allows you to remove an Interceptor or advice from a pointcut,

as well as apply new Interceptors and Advice to existing pointcuts.

Aspect 管理員視窗是將 AOP 描述檔案(jboss-aop.xml)圖像化. 而它亦可在方法中運用 Interceptor 或 Advice, 和由 Pointcut 刪除 Interceptor 或 Advice.

在主目錄欄中選 ´Window´ -> ´Show View´ -> ´Other...´

在彈出的視窗中會看到 ´JBoss AOP´ 按 ´+´ 擴展後選 ´Advised Manager´

完成後會在 eclipse 下方中顯示 Aspect 管理員視窗.

 

 

出處: http://docs.jboss.org/aop/aspect-framework/reference/en/html/aopide.html#aopide-install