?? jb7-1-3.htm
字號:
<HTML>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb2312">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<TITLE>Enterprise JavaBeans Developer's Guide: Creating enterprise beans with JBuilder</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<META content=LDJ name=Author>
<META content="Microsoft FrontPage 4.0" name=GENERATOR>
<META content="steeven (phpme@citiz.net) 9/8/2001" name=author><LINK
href="file:///E:/jbdocs.css" rel=stylesheet type=text/css></HEAD>
<BODY bgcolor="#fefef2" style="FONT-SIZE: 9pt"><A name=creating_enterprise_beans_with_jbuilder></A>
<H1><STRONG><FONT color=#0080FF size=3 >用JBuilder生成enterprise beans</FONT></STRONG>
</H1>
<hr>
<!--BNDX="EJB groups:defined;EJB groups:file extensions;EJB groups:types"-->
譯者名字: steeven<br>
郵件地址:<A href="phpme_40citiz.net">phpme@citiz.net</A>
<P>你要建立的enterprise bean必須屬于一個JBuilder EJB group. EJB
group是那些要部署在同一JAR文件中的beans在邏輯上的組。 它包含了建立要建立的JAR文件的deployment
descriptor(s)(部署描述)的信息。可以使用Delployment Descriptor editor來編輯EJB group的內容。</P>
<P>一旦你用建立并且用<A
href="file:///E:/enterprise/beans_with_jbuilder.html#editing_the_deployment_descriptor">Deployment
Descriptor editor</A>修改后,你就可以Make或者Build EJB group來生成JAR文件。JBuilder使用deployment
descriptor來幫助標志要打包的文件。</P>
<P>根據擴展名的不同,一個EJB group可以是下面的兩種格式之一:</P>
<TABLE border=1 cellspacing="0" cellpadding="0">
<TBODY>
<TR>
<TD vAlign=top>.ejbgrp</TD>
<TD vAlign=top>
<P>二進制文件</P></TD></TR>
<TR>
<TD vAlign=top>.ejbgrpx</TD>
<TD vAlign=top>
<P>XML文件。同.ejbgrp文件保持相同的內容,但因為是文本文件,更容易同版本控制系統配合。</P></TD></TR></TBODY></TABLE>
<P>在一個project中可包含多個EJB group。它們使用相同的classpath和JDK, 并且它們被配置成面向同一application
server.</P>
<P>如果你還沒有這樣配置,請按照上一章, <A
href="file:///E:/enterprise/target.html">"設定目的Application server."</A>
你必須按照步驟,并且加入你application server的library.<BR><A name=creating_an_ejb_group></A>
<hr>
<p><font color="#000080" size="3"><b>建立EJB group</b></font></p><!--BNDX="EJB groups:creating"-->
<P>有兩個辦法可以建立EJB group:</P>
<UL>
<LI>如果還沒有建立enterprise bean, 可使用Empty
EJB Group向導來建立。<BR>
<LI>可用EJB Group
From Descriptors向導從已有的enterprise bean建立EJB deployment descriptors的時候創建。
</LI></UL>
<P> <A
name=creating_an_empty_ejb_group></A></P>
<P>如果你在使用EJB
group向導前還沒有打開的project,JBuilder會先顯示Project向導。Project創建以后才會顯示你選擇的EJB向導。</P>
<p><font size="2" color="#000080"><b>建立一個空的EJB group</b></font></p><!--BNDX="EJB groups:defined;EJB groups:empty;Empty EJB Group wizard;wizards:Empty EJB Group;EJB wizards:Empty EJB Group"-->
<P>如果你還沒有建立你的enterprise beans, 請從建立一個空的EJB group開始。</P>
<OL>
<LI>Choose File|New and click the Enterprise tab.選擇菜單FIle|New
并且單擊Enterprise項。<BR><BR><SPAN
class=bscan><STRONG><font color="#FF0000">注意</font>:</STRONG> </SPAN>如果EJB向導不可選,是因為你還沒有設定你的application
server. 請參照<A href="file:///E:/enterprise/target.html">"設定目的application
server"</A> 來獲得詳細信息.<BR><BR>
<LI>雙擊Empty EJB Group向導的圖標出現該向導:<BR><BR>
<P><IMG alt="" border=0 height=401
src="images/ejbgroup_wizard.gif" width=581></P>
<LI>指定EJB group的名稱<BR><BR>
<LI>指定新group的類型<BR><BR>
ejbgrp以.zip格式保存deployment
descriptors,并且在JBuilder5以前的版本中使用。jebgrpx用XML格式保存deployment
descriptors。XML格式允許用戶在代碼控制系統中(VSS,CVS)合并所做的改變。推薦使用ejpgrpx,除非你要同舊版的JBuilder勾搭,
<LI>指定JAR文件的名字<BR>
<BR>JBuilder缺省為同Ejb
group的名字相同。可簡單接受,也可自行指定。Jbuilder也根據project路徑產生缺省路徑,也可自行指定。
<LI>點擊OK來生成EJB group。 </LI></OL><A
name=creating_ejb_group_from_existing_beans></A>
<p><font color="#000080" size="2"><b>為已有的enterprise beans建立EJB group</b></font></p><!--BNDX="EJB Group From Descriptors wizard;EJB groups:from deployment descriptors;wizards:EJB Group From Descriptors;EJB wizards:EJB Group From Descriptors"-->
<P>如果你已經有了BAS enterprise beans, 可按一下步驟把它們加入一個EJB
group:</P>
<OL>
<LI>選擇菜單中File|New,點擊Enterprise項。<BR><BR>
<LI>雙擊EJB Group From Descriptors向導。<BR><BR>
<P><IMG alt="" border=0 height=401
src="images/ejb_group_descriptors.gif" width=581></P>
<LI>指定EJB group的名字。<BR><BR>
<LI>指定新group的類型
<P>ejbgrp以.zip格式保存deployment
descriptors,并且在JBuilder5以前的版本中使用。jebgrpx用XML格式保存deployment
descriptors。XML格式允許用戶在代碼控制系統中(VSS,CVS)合并所做的改變。推薦使用ejpgrpx,除非你要同舊版的JBuilder勾搭</P>
<LI>指定JAR文件的名字和路徑。
<P>JBuilder自動缺省名字為EJB group的名字。可接受或者自行指定。</P>
<LI>點擊Next并且指定包含deployment
descriptor的路徑。指定后向導會列出此路徑下可用的deployment descriptors<BR><BR>
<P><IMG alt="" border=0 height=427
src="images/ejb_group_descriptors2.gif" width=607></P>
<LI>點擊Finish生成EJB group來包含bean的deployment
descriptors。 </LI></OL><A name=creating_an_enterprise_bean></A>
<hr>
<p><b><font color="#000080" size="3">建立enterprise bean</font></b></p><!--BNDX="enterprise beans:creating with wizards;Enterprise JavaBean wizard;EJB wizards:Enterprise JavaBean;wizards:Enterprise JavaBean"-->
<P>JBuilder的ojbect建立向導中包含兩個生成enterprise bean的向導:Enterprise JavaBean向導和EJB Entity
Bean Modeler. 在菜單Wizards中還包含:EJB Bean Genertor. 本節討論Enterprise
JavaBean向導的使用。<BR></P>
<P>The Enterprise JavaBean向導和EJB Entity Bean Modeler向導都采用了同時建立enterpise
bean和home,remote接口的方式。如果你想從已建立的remote interface上開始開發enterprise bean, 請參考<A
href="file:///E:/enterprise/beans_with_jbuilder.html#generating_the_bean_class_from_the_remote">"從remote
interface建立bean"</A>來了解如何用EJB Bean Generator來從remote 接口建立你的bean class。<BR></P>
<P>利用Enterprise JavaBean向導建立enterprise bean,</P>
<OL>
<LI>選擇菜單File|New,點擊Enterprise項<BR><BR>
<LI>雙擊Enterprise
JavaBean向導圖標。<BR><BR>
向導出現。
<P><IMG alt="" border=0 height=448
src="images/ejbwiz.gif" width=550></P>
<LI>在下拉列表中,選擇你喜歡enterprise bean要屬于哪個EJB group。 選擇Next顯示下一頁。<BR><BR>
<P>如果你以前沒有定義EJB
group或者你想建立新的EJB group, 單擊New按鈕。在建立enterprise bean之前必須至少有一個定義好的EJB group。</P>
<LI>
<P><IMG alt="" border=0 height=461
src="images/ejb_wiz2.gif" width=558></P>
<LI>指定bean class的名稱,所在的package和bean的基類。 </LI></OL>
<P>下一步你必須決定要建立session bean還是entity bean.</P>
<P><A name=creating_a_session_bean></A></P>
<p><font color="#000080" size="2"><b>建立session bean</b></font></p>
<P>如果你要建立session bean,</P>
<OL>
<LI>點擊Stateless Session Bean(無狀態)或者Stateful Session Bean(有狀態).
<P>要了解更多的session bean的類型, 請參考"開發session beans"一章中的<A
href="file:///E:/enterprise/session_beans.html#types_of_session_beans">"Types
of session beans"</A>。</P>
<LI>如果你選擇了有Stateful Session Bean, 還可以核選Session
Synchronization復選框來實現 <CODE>SessionSynchronization</CODE>接口。
<P>要了解<CODE><font size="2">SessionSynchronization</font></CODE>接口,請參考"Developing session
beans"中的<A
href="file:///E:/enterprise/session_beans.html#the_session_synchronization_interface">"The
SessionSynchronization interface"</A>。</P>
<LI>單擊Next進入第三步。<BR><BR>
<P><IMG alt="" border=0 height=461
src="images/ejb_step3.gif" width=558></P>
<LI>指定Home接口,Remote接口和Bean
Home的名字。Jubilder根據bean的名字產生缺省值。<BR><BR>
<LI>單擊Finish。 </LI></OL><A name=creating_an_entity_bean></A>
<p><BR><font color="#000080" size="2"><b>建立entity bean</b></font></p>
<P>如果你要建立entity bean,</P>
<OL>
<LI>選擇Bean Managed Persistence Entity
Bean選項或者Container Managed Persistence 1.1 Entity Bean選項。
<P>要了解bean-managed和container-managed持久性,請參閱"Developing entity
beans"中的<A
href="file:///E:/enterprise/entity_beans.html#persistence_and_entity_beans">"Persistence
and entity beans"</A>。</P>
<LI>指定Primary Key Class。<BR><BR>
<LI>單擊Next進入步驟三。<BR><BR>
<P><IMG alt="" border=0 height=461
src="images/ejb_entity_step3.gif" width=558></P>
<LI>指定Home接口,Remote接口和Bean
Home的名字。Jubilder根據bean的名字產生缺省值。<BR><BR>
<LI>單擊Finish. </LI></OL>
<P>單擊Finish按鈕后,JBuilder建立bean
class和它的home,remote接口。可以看到它們出現在project面版中。察看源代碼你會發現session
bean實現的是<CODE>SessionBean</CODE>接口,entity
bean實現的是EntityBean接口。JBuilder已經加入了一些空的方法,也就是在每個enterprise
bean中都必須實現的方法。你可以在這些方法中加入代碼來實現調用這些方法所要完成的邏輯。</P>
<P>home接口擴展了EJBHome接口并且包含了一個建立bean所需要的create()方法。remote接口擴展了EJBObject,因為你還沒有聲明任何邏輯方法,所以是空的。</P>
<P>盡管你可以用Enterprise JavaBeans向導開始你的entity bean,推薦用<A
href="file:///E:/enterprise/entity_modeler.html">EJB Entity Bean
Modeler</A>向導。除非你對Enterprise JavaBean生成的Entity bean做了完善, 否則無法通過Deployment
Descriptor editor的驗證。</P><A name=adding_the_business_logic></A>
<p><font color="#000080" size="2"><b>為你的bean加入商業邏輯</b></font></p><!--BNDX="business methods:adding to enterprise bean;Bean designer:Properties page;adding properties to EJB;adding business methods to EJB"-->
<P>在bean
class的代碼中定義方法來實現enterprise bean需要的邏輯。</P>
<P>如果你要給bean加入屬性,可直接在代碼中加入,也可使用于Bean designer中的Properties頁。</P>
<P>要使用Bean
designer來建立屬性,</P>
<OL>
<LI>在項目面版中雙擊bean class。
<LI>單擊Bean頁來顯示Bean
designer。
<LI>單擊Properties頁。
</LI></OL>
<p><img border="0" src="images/ejb_wiz_properties.gif" width="805" height="568"></p>
<P>要加入新的property,</P>
<OL>
<LI>單擊Add Property按鈕來顯示New Property對話窗。<BR><BR>
<P><IMG alt="" border=0 height=289
src="images/new_prop.gif" width=450></P>
<LI>指定Property的名字和屬性。<BR><BR>
<LI>通過設定Getter和Setter選項來指定訪問的方法。
<P>如果你覺得你的property需要getter訪問方法,你也可以覺得是否要它出現在bean
class和/或者在remote接口中。</P>
<LI>選擇Apply來立即在bean代碼中加入新property的定義。你指定的訪問方法被被加入到bean
class/remote接口中。<BR><BR>
<LI>你可以在對話框中連續加入新的property。按OK結束。 </LI></OL>
<P>如果你使用Enterprise JavaBean向導來建立一個container-managed persistence entity bean,
你要在在bean中加入property. 注意其中的一個property必須被設置成primary key并且在Deployment Descriptor
editor的<A
href="file:///E:/enterprise/editing_deploymentdescriptors.html#persistence_panel">Persistence
panel</A>中指定哪個或者哪些field組成了primary key。如果您沒有這樣做,Deployment Descriptor editor就無法<A
href="file:///E:/enterprise/editing_deploymentdescriptors.html#verifying_descriptor_information">驗證deployment
descriptor</A>是有效。</P>
<P>你也可以利用Properties頁面來修改property。例如,如果你在建立property的時候沒有指定一個setter,現在又決定為bean添加一個,你可以簡單的在Properties頁面為這個property核選Setter,JBuilder會加入這個setter方法到你的代碼中。或者你可以用去掉getter或者setter方法,只要不要核選那個復選框即可。</P>
<P>在Propertyis頁面移除bean的一個property</P>
<OL>
<LI>選中property列表中的那個property。<BR><BR>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -