📄 ejbconcepts6.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Defining Client Access with Interfaces</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="EJBConcepts5.html" /> <link rel="Next" href="EJBConcepts7.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="EJBConcepts5.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="EJBConcepts7.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp80011"> </a><h2 class="pHeading1">Defining Client Access with Interfaces</h2><a name="wp80012"> </a><p class="pBody">The material in this section applies only to session and entity beans, not to message-driven beans. Because they have a different programming model, message-driven beans do not have interfaces that define client access. </p><a name="wp80014"> </a><p class="pBody">A client may access a session or an entity bean only through the methods defined in the bean's interfaces. These interfaces define the client's view of a bean. All other aspects of the bean--method implementations, deployment descriptor settings, abstract schemas, and database access calls--are hidden from the client.</p><a name="wp80017"> </a><p class="pBody">Well-designed interfaces simplify the development and maintenance of J2EE applications. Not only do clean interfaces shield the clients from any complexities in the EJB tier, but they also allow the beans to change internally without affecting the clients. For example, even if you change your entity beans from bean-managed to container-managed persistence, you won't have to alter the client code. But if you were to change the method definitions in the interfaces, then you might have to modify the client code as well. Therefore, to isolate your clients from possible changes in the beans, it is important that you design the interfaces carefully.</p><a name="wp80018"> </a><p class="pBody">When you design a J2EE application, one of the first decisions you make is the type of client access allowed by the enterprise beans: remote, local, or Web service. </p><a name="wp80020"> </a><h3 class="pHeading2">Remote Clients</h3><a name="wp80021"> </a><p class="pBody">A remote client of an enterprise bean has the following traits:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80022"> </a><div class="pSmartList1"><li>It may run on a different machine and a different Java virtual machine (JVM) than the enterprise bean it accesses. (It is not required to run on a different JVM.)</li></div><a name="wp80023"> </a><div class="pSmartList1"><li>It can be a Web component, a J2EE application client, or another enterprise bean.</li></div><a name="wp80024"> </a><div class="pSmartList1"><li>To a remote client, the location of the enterprise bean is transparent. </li></div></ul></div><a name="wp80025"> </a><p class="pBody">To create an enterprise bean with remote access, you must code a remote interface and a home interface. The <span style="font-style: italic">remote interface</span> defines the business methods that are specific to the bean. For example, the remote interface of a bean named <code class="cCode">BankAccountEJB</code> might have business methods named deposit and credit. The<span style="font-style: italic"> home interface</span> defines the bean's life cycle methods--<code class="cCode">create</code> and <code class="cCode">remove</code>. For entity beans, the home interface also defines finder methods and home methods. Finder methods are used to locate entity beans. Home methods are business methods that are invoked on all instances of an entity bean class. <a href="EJBConcepts6.html#wp80038">Figure 18-2</a> shows how the interfaces control the client's view of an enterprise bean.</p><a name="wp80036"> </a><p class="pBody"></p><div align="left"><img src="images/Fig92.gif" height="203" width="417" alt="Interfaces for an Enterprise Bean With Remote Access" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="80038"> </a><strong><font >Figure 18-2 Interfaces for an Enterprise Bean With Remote Access</font></strong></p><a name="wp80040"> </a><h3 class="pHeading2">Local Clients</h3><a name="wp80041"> </a><p class="pBody">A local client has these characteristics:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80042"> </a><div class="pSmartList1"><li>It must run in the same JVM as the enterprise bean it accesses.</li></div><a name="wp80043"> </a><div class="pSmartList1"><li>It may be a Web component or another enterprise bean.</li></div><a name="wp80044"> </a><div class="pSmartList1"><li>To the local client, the location of the enterprise bean it accesses is not transparent.</li></div><a name="wp80046"> </a><div class="pSmartList1"><li>It is often an entity bean that has a container-managed relationship with another entity bean.</li></div></ul></div><a name="wp80047"> </a><p class="pBody">To build an enterprise bean that allows local access, you must code the local interface and the local home interface. The <span style="font-style: italic">local interface</span> defines the bean's business methods, and the <span style="font-style: italic">local home</span> interface defines its life cycle and finder methods.</p><a name="wp80051"> </a><h3 class="pHeading2">Local Interfaces and Container-Managed Relationships</h3><a name="wp80052"> </a><p class="pBody">If an entity bean is the target of a container-managed relationship, then it must have local interfaces. The direction of the relationship determines whether or not a bean is the target. In <a href="EJBConcepts4.html#wp79919">Figure 18-1</a>, for example, <code class="cCode">ProductEJB</code> is the target of a unidirectional relationship with <code class="cCode">LineItemEJB</code>. Because <code class="cCode">LineItemEJB</code> accesses <code class="cCode">ProductEJB</code> locally, <code class="cCode">ProductEJB</code> must have the local interfaces. <code class="cCode">LineItemEJB</code> also needs local interfaces--not because of its relationship with <code class="cCode">ProductEJB</code>--but because it is the target of a relationship with <code class="cCode">OrderEJB</code>. And because the relationship between <code class="cCode">LineItemEJB</code> and <code class="cCode">OrderEJB</code> is bidirectional, both beans must have local interfaces.</p><a name="wp80057"> </a><p class="pBody">Because they require local access, entity beans that participate in a container-managed relationship must reside in the same EJB JAR file. The primary benefit of this locality is increased performance--local calls are usually faster than remote calls.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -