⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
 </LI><LI CLASS="BL"><A NAME="pgfId-1087703"></A> <EM CLASS="CODE">SellerHome</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087704"></A> <EM CLASS="CODE">SellerBean</EM> </LI></UL></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087706"></A><A NAME="59664"></A>Container Classes</H4><P CLASS="Body"><A NAME="pgfId-1087709"></A><A NAME="marker-1087707"></A><A NAME="marker-1087708"></A>The container needs classes to deploy an enterprise bean onto a particular Enterprise JavaBeans server, and those classes are generated with a deployment tool. Container classes include *<EM CLASS="CODE">_Stub.class</EM> and *<EM CLASS="CODE">_Skel.class</EM> classes that provide the RMI hooks on the client and server sides for marshaling (moving) data between the client program and the Enterprise JavaBeans server. In addition, implementation classes are created for the interfaces and deployment rules defined for each bean. </P><UL><LI CLASS="BL"><A NAME="pgfId-1087711"></A><A NAME="marker-1087710"></A>The stub object is installed on or downloaded to the client system and provides a local proxy object for the client. It implements the remote interfaces and transparently delegates all method calls across the network to the remote object. </LI><LI CLASS="BL"><A NAME="pgfId-1087713"></A><A NAME="marker-1087712"></A>The skel object is installed on or downloaded to the server system and provides a local proxy object for the server. It unwraps data received over the network from the stub object for processing by the server. </LI></UL></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087715"></A><A NAME="28310"></A>Examining a Container-Managed Bean</H4><P CLASS="Body"><A NAME="pgfId-1087718"></A><A NAME="marker-1087716"></A>This section walks through <A NAME="marker-1087717"></A>the<EM CLASS="CODE"> RegistrationBean</EM> code to show how easy it is to have the container manage <A NAME="marker-1087719"></A>persistent data storage to an underlying medium such as a database (the default). Chapter 3, Data and Transaction Management modifies <EM CLASS="CODE">RegistrationBean</EM> to use bean-managed persistence to handle database access and manage transactions. </P><DIV><H5 CLASS="B"><A NAME="pgfId-1087720"></A>Member Variables</H5><P CLASS="Body"><A NAME="pgfId-1087723"></A><A NAME="marker-1087721"></A>A <A NAME="marker-1087722"></A>container-managed environment needs to know which variables are for persistent storage and which are not. In the Java programming language, the <EM CLASS="CODE">transient</EM><A NAME="marker-1087724"></A> keyword indicates variables to not include when data in an object is serialized and written to <A NAME="marker-1087725"></A>persistent storage. In the <EM CLASS="CODE">RegistrationBean</EM> class, the <EM CLASS="CODE">EntityContext</EM> variable is marked transient to indicate that its data not be written to the underlying storage medium. </P><P CLASS="Body"><A NAME="pgfId-1087726"></A><EM CLASS="CODE">EntityContext</EM> data is not written to persistent storage because its purpose is to provide information on the container's run-time context. It, therefore, does not contain data on the registered user and should not be saved to the underlying storage medium. The other variables are declared <EM CLASS="CODE">public</EM> so the container can use the Reflection API to discover them. </P><PRE CLASS="CODE"><A NAME="pgfId-1087727"></A>  protected transient EntityContext ctx;  public String theuser, password, creditcard, emailaddress;  public double balance;</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087728"></A>Create Method</H5><P CLASS="Body"><A NAME="pgfId-1087732"></A><A NAME="marker-1087729"></A>The bean's <EM CLASS="CODE">ejbCreate</EM><A NAME="marker-1087730"></A> method is <A NAME="marker-1087731"></A>called by the container after the client program calls the <EM CLASS="CODE">create</EM> method on the remote interface and passes in the registration data. This method assigns the incoming values to the member variables that represent user data. The container handles storing and loading the data, and creating new entries in the underlying storage medium. </P><PRE CLASS="CODE"><A NAME="pgfId-1087733"></A>public RegistrationPK ejbCreate(String theuser,String password,                                String emailaddress,String creditcard)                                throws CreateException, RemoteException {  this.theuser=theuser;  this.password=password;  this.emailaddress=emailaddress;  this.creditcard=creditcard;  this.balance=0;</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087734"></A>Entity Context Methods</H5><P CLASS="Body"><A NAME="pgfId-1087738"></A><A NAME="marker-1087735"></A>An entity bean has an associated<A NAME="marker-1087736"></A> <EM CLASS="CODE">EntityContext</EM><A NAME="marker-1087737"></A> instance that gives the bean access to container-managed run-time information such as the transaction context. </P><PRE CLASS="CODE-caption"><A NAME="pgfId-1087740"></A>//API Ref: <A NAME="56846"></A>void setEntityContext(EntityContext ectx)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087741"></A>    <A NAME="javax.ejb.EntityBean class"></A><A NAME="EntityBean class"></A><A NAME="setEntityContext method"></A><A NAME="unsetEntityContext method"></A>public void setEntityContext(javax.ejb.EntityContext ctx)                                  throws RemoteException {    this.ctx = ctx;  }</PRE><PRE CLASS="CODE-caption"><A NAME="pgfId-1087747"></A>//API Ref: <A NAME="49999"></A>void unsetEntityContext(EntityContext ectx)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087748"></A>  public void unsetEntityContext() throws RemoteException{    ctx = null;  <EM CLASS="A">}</EM></PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087749"></A>Load Method</H5><P CLASS="Body"><A NAME="pgfId-1087753"></A>The bean's <A NAME="marker-1087750"></A><EM CLASS="CODE">ejbLoad</EM><A NAME="marker-1087751"></A> <A NAME="marker-1087752"></A>method is called by the container to load data from the underlying storage medium. This would be necessary when <EM CLASS="CODE">BidderBean</EM> or <EM CLASS="CODE">SellerBean</EM> need to check a user's ID or password against the stored values. </P><P CLASS="Body"><A NAME="pgfId-1087754"></A>You do not implement the <EM CLASS="Code">ejbLoad</EM> method because the Enterprise JavaBeans container seamlessly loads the data from the underlying storage medium for you. </P><PRE CLASS="CODE-caption"><A NAME="pgfId-1087756"></A>//API Ref: <A NAME="54794"></A>void ejbLoad()</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087759"></A>  <A NAME="Entity Bean class"></A><A NAME="ejbLoad method"></A>public void ejbLoad() throws RemoteException {}</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087760"></A>Store Method</H5><P CLASS="Body"><A NAME="pgfId-1087764"></A>The <A NAME="marker-1087761"></A>bean's <EM CLASS="CODE">ejbStore</EM><A NAME="marker-1087762"></A> <A NAME="marker-1087763"></A>method is called by the container to save user data. This method is not implemented because the Enterprise JavaBeans container seamlessly stores the data to the underlying storage medium. </P><PRE CLASS="CODE-caption"><A NAME="pgfId-1087766"></A>//API Ref: <A NAME="11020"></A>void ejbStore()</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087768"></A>  <A NAME="ejbStore method"></A>public void ejbStore() throws RemoteException {}</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087769"></A>Connection Pooling</H5><P CLASS="Body"><A NAME="pgfId-1087770"></A>Loading data from and storing data to a database can take a lot of time and reduce an application's overall performance. To reduce database connection time, the BEA Weblogic server uses a JDBC connection pool to cache database connections so connections are always available when the application needs them. </P><P CLASS="Body"><A NAME="pgfId-1087772"></A><A NAME="marker-1087771"></A>However, you are not limited to the default JDBC connection pool. You can override the bean-managed connection pooling behavior and substitute your own. Chapter 8, Performance Techniques, explains how. </P></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087773"></A>Deployment Descriptor</H5><P CLASS="Body"><A NAME="pgfId-1087777"></A><A NAME="marker-1087774"></A>The remaining <A NAME="marker-1087775"></A><A NAME="marker-1087776"></A>configuration for a container-managed bean occurs at deployment time. The following is the text-based deployment descriptor used in a BEA Weblogic Enterprise JavaBeans server for deploying the <EM CLASS="CODE">Registration</EM> Bean. </P><PRE CLASS="CODE"><A NAME="pgfId-1087778"></A>Text Deployment Descriptor</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087779"></A>  (environmentProperties    (persistentStoreProperties      persistentStoreType          jdbc      (jdbc        tableName                  registration        dbIsShared                 false        poolName                   ejbPool        (attributeMap          creditcard               creditcard          emailaddress             emailaddress          balance                  balance          password                 password          theuser                  theuser        ); end attributeMap      ); end jdbc    ); end persistentStoreProperties</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087780"></A>  ); end environmentProperties</PRE><P CLASS="Body"><A NAME="pgfId-1087781"></A>The deployment descriptor indicates that storage is a database whose connection is held in a JDBC connection pool called <EM CLASS="CODE">ejbPool</EM>. The <EM CLASS="CODE">attributeMap</EM> contains the enterprise bean variable on the left and the associated database field on the right. </P></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087782"></A>XML Deployment Descriptor</H5><P CLASS="Body"><A NAME="pgfId-1087785"></A><A NAME="marker-1087783"></A><A NAME="marker-1087784"></A>In Enterprise JavaBeans 1.1, the deployment descriptor uses XML. Below is the equivalent configuration in XML for the <EM CLASS="CODE">Registration</EM> Bean. </P><P CLASS="Body"><A NAME="pgfId-1087786"></A>The container-managed fields map directly to their counterpart names in the database table. The container resource authorization (res-auth) means the container handles the database login for the <EM CLASS="CODE">REGISTRATION</EM> table. </P><PRE CLASS="CODE"><A NAME="pgfId-1087787"></A>&lt;persistence-type&gt;Container&lt;/persistence-type&gt;&lt;cmp-field&gt;&lt;field-name&gt;creditcard    &lt;/field-name&gt;&lt;/cmp-field&gt;&lt;cmp-field&gt;&lt;field-name&gt;emailaddress    &lt;/field-name&gt;&lt;/cmp-field&gt;&lt;cmp-field&gt;&lt;field-name&gt;balance    &lt;/field-name&gt;&lt;/cmp-field&gt;&lt;cmp-field&gt;&lt;field-name&gt;password    &lt;/field-name&gt;&lt;/cmp-field&gt;&lt;cmp-field&gt;&lt;field-name&gt;theuser    &lt;/field-name&gt;&lt;/cmp-field&gt;&lt;resource-ref&gt;&lt;res-ref-name&gt;registration&lt;/res-ref-name&gt;&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;&lt;res-auth&gt;Container&lt;/res-auth&gt;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087788"></A>&lt;/resource-ref&gt;</PRE></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087790"></A><A NAME="38015"></A>Container-Managed Finder Methods</H4><P CLASS="Body"><A NAME="pgfId-1087795"></A><A NAME="marker-1087791"></A><A NAME="marker-1087792"></A><A NAME="marker-1087793"></A><A NAME="marker-1087794"></A>The auction house search facility is implemented as a container-managed finder method. It starts when the <A NAME="marker-1087796"></A>end user types in a search string and clicks the Submit button on the home page to locate an auction item. This section walks through the different parts of the finder-based search code. Chapter 3 describes how to create a bean-managed search to handle complex queries and searches that span more than one bean type (entity and session beans) or database tables.</P><DIV><H5 CLASS="B"><A NAME="pgfId-1087797"></A>Finder-Based Search</H5><P CLASS="Body"><A NAME="pgfId-1087799"></A>Figure 2.5 shows how the browser passes the search string to the <EM CLASS="CODE">AuctionServlet.searchItem</EM><A NAME="marker-1087798"></A> method, which then passes it to the <EM CLASS="CODE">BidderBean.getMatchingItemsList</EM><A NAME="marker-1087800"></A> method. At this point, <EM CLASS="CODE">BidderBean.getMatchingItemsList</EM> passes the search string to the <EM CLASS="CODE">findAllMatchingItems</EM><A NAME="marker-1087801"></A> method declared in the <EM CLASS="CODE">AuctionItemHome</EM> interface. </P><DIV><H6 CLASS="FC"><A NAME="pgfId-1087806"></A>Figure 2.5 <A NAME="17103"></A>Searching for auction items</H6><DIV><IMG SRC="CH02-5.gif"></DIV>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -