溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

J2EE如何創(chuàng)建Enterprise Bean

發(fā)布時(shí)間:2022-01-10 11:21:24 來(lái)源:億速云 閱讀:118 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“J2EE如何創(chuàng)建Enterprise Bean”,在日常操作中,相信很多人在J2EE如何創(chuàng)建Enterprise Bean問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”J2EE如何創(chuàng)建Enterprise Bean”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

enterprise bean 是一個(gè)包含應(yīng)用程序商務(wù)邏輯的服務(wù)端組件.在運(yùn)行時(shí)期, 應(yīng)用程序客戶端調(diào)用enterprise bean的方法執(zhí)行商務(wù)邏輯.在我們的例子中enterprise bean是一個(gè)稱為ConverterEJB的無(wú)狀態(tài)session bean. ConverterEJB bean的源碼在examples/src/ejb/converter目錄中.

編寫Enterprise Bean

這個(gè)例子中的enterprise bean需要下面的代碼:

  • Remote interface

  • Home interface

  • Enterprise bean class

編寫Remote Interface

remote interface 定義客戶端可以調(diào)用的商務(wù)方法. 商務(wù)方法在enterprise bean中實(shí)現(xiàn). 下面是Converterremote interface 的源代碼.

import Javax.ejb.EJBobject; import java.Rmi.RemoteException; public interface Converter extends EJBObject { public double dollarToYen(double dollars) throws RemoteException; public double yenToEuro(double yen) throws RemoteException; }


編寫Home Interface

home interface定義允許客戶端去創(chuàng)建, 查找,或移除enterprise bean的方法. ConverterHome interface包含單個(gè)create方法,它返回一個(gè)remote interface類型的對(duì)象.這是ConverterHome接口的源碼:

import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface ConverterHome extends EJBHome { Converter create() throws RemoteException, CreateException; }


編寫Enterprise Bean Class

例子中的enterprise bean class稱為 ConverterBean.這個(gè)類實(shí)現(xiàn)兩個(gè)商務(wù)方法, dollarToYenyenToEuro, 它們由Converter remote interface定義.下面是ConverterBean類的源碼.

import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class ConverterBean implements SessionBean { public double dollarToYen(double dollars) { return dollars * 121.6000; } public double yenToEuro(double yen) { return yen * 0.0077; } public ConverterBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} }


編譯源文件

現(xiàn)在可以準(zhǔn)備去編譯remote interface(Converter.java), home interface (ConverterHome.java),和enterprise bean類 (ConverterBean.java):

  1. examples/src 目錄.

  2. 在終端窗口鍵入下列命令:

    ant converter


這個(gè)命令編譯enterprise bean和J2EE應(yīng)用程序客戶端的源文件 . 它把生成的類文件放在examples/build/ejb/converter目錄中. 要獲得更多關(guān)于ant的信息,查看怎樣建立和運(yùn)行例子.


注意: 當(dāng)編譯代碼的時(shí)候,ant需要包含在classpath中的j2ee.jar文件. 這個(gè)文件放在J2EE SDK安裝的lib目錄下. 如果你打算使用其它的工具去編譯J2EE組件的源代碼,確認(rèn)在classpath中包括j2ee.jar 文件.


打包Enterprise Bean

在這個(gè)章節(jié)中你將運(yùn)行deploytool的New Enterprise Bean Wizard 去執(zhí)行這些任務(wù):

  • 創(chuàng)建the bean's deployment descriptor.

  • 在一個(gè)EJB JAR文件中打包deployment descriptor和bean的類.

  • 嵌入EJB JAR文件到應(yīng)用程序的ConverterApp.ear文件中.

要開(kāi)始New Enterprise Bean Wizard,選擇File->New Enterprise Bean. 向?qū)э@示下面對(duì)話框.

  1. Introduction對(duì)話框

    1. 閱讀向?qū)匦愿庞[的說(shuō)明文本.

    2. 單擊Next.

  2. EJB JAR對(duì)話框

    1. 在應(yīng)用程序按鈕中選擇Create new EJB File.

    2. 在組合框中,選擇ConverterApp.

    3. 在EJB Display Name的欄中輸入ConverterJAR.

    4. 單擊 Edit.

    5. 在Available Files的目錄樹(shù)下,找到examples/build/ejb/converter目錄.(如果converter目錄在樹(shù)的多層下,你可以在Starting Directory欄輸入全部或部分converter的目錄路徑名以簡(jiǎn)化樹(shù)的視圖.)

    6. 從Available Files目錄樹(shù)中選擇下面的類 然后單擊Add: Converter.class, ConverterBean.class, ConverterHome.class. (你也可以拖動(dòng)這些類文件到Contents text區(qū)域.)

    7. 單擊OK.

    8. 單擊Next.

  3. 常規(guī)對(duì)話框

    1. 在Bean類型下,選擇Session單選按鈕.

    2. 選擇Stateless單選按鈕.

    3. 在Enterprise Bean Class組合框中,選擇ConverterBean.

    4. 在Enterprise Bean Name欄, 輸入 ConverterEJB.

    5. 在Remote Home Interface組合框,選擇ConverterHome.

    6. 在Remote Interface組合框,選擇Converter.

    7. 單擊Next.

  4. 事務(wù)管理對(duì)話框

  • 因?yàn)槟憧梢院雎允O碌膶?duì)話框,直接單擊Finish.

到此,關(guān)于“J2EE如何創(chuàng)建Enterprise Bean”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI