📄 ch07s10.html
字号:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Specifying the deployment name of your beans</title><link rel="stylesheet" href="styles.css" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/styles.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets Vimages/callouts/"><link rel="home" href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html" title="JBoss 3.0 Documentation"><link rel="up" href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html" title="Chapter 7. Advanced container configuration : use of jboss.xml"><link rel="previous" href="ch07s02.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s02.html" title="Declaring Resource Reference"><link rel="next" href="ch07s13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s13.html" title="Declaring an EJB reference"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="jboss.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jboss.gif" border="0"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch07s02.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s02.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch07s13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s13.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table><div class="section"><a name="d0e2620"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="d0e2620"></a>Specifying the deployment name of your beans</h2></div></div><p>You have coded your beans. You now want to deploy them and be
able to access them from your clients. For this, JBoss will register your beans in the JNDI namespace. All you have to tell is the name under which they must be registered.</p><div class="section"><a name="d0e2625"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2625"></a>Standard behaviour of jboss</h3></div></div><p>The simplest way to deploy your bean is to use the ejb-jar.xml file to give it
the same name you
call it in the client. This is done in the <ejb-name> tag for the
bean. In this case you DON'T
need a jboss.xml file. Your ejb-jar.xml file will look like this (this is
covered in the beginners trails):
</p><p>ejb-jar.xml: </p><pre class="programlisting">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>MyBeanName</ejb-name>
<home>MyBeanHome</home>
<remote>MyBean</remote>
<ejb-class>MyBeanEJB</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
</pre><p>And your bean will be available under the "MyBeanName" in JNDI.
In your client code, your call will look like this:</p><pre class="programlisting">import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class Client {
public static void main(String args[]) {
...
InitialContext namingContext = new InitialContext();
Object ref = namingContext.lookup("MyBeanName");
MyBeanHome home = (MyBeanHome)PortableRemoteObject.narrow(ref, MyBeanHome.class);
...
}
}</pre><p>Here the "MyBeanName" refers to the name of your bean in the JNDI namespace.
JBoss does not currently allow
you to use the java:comp/env namespace to call your beans from your clients.</p></div><div class="section"><a name="d0e2640"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2640"></a>Registering a bean with a JNDI deployment name different than the ejb-name</h3></div></div><p>You may want to deploy your bean under another JNDI name. (for example, you
may want to deploy the same
bean under different names with different configurations). In this case, you
must specify the JNDI name
in a jboss.xml file. This file must be in the META-INF directory, along with
ejb-jar.xml. Your files
will look like this (note that the <ejb-name>tags in the two xml
files must match):
</p><p>jboss.xml:</p><pre class="programlisting">
<jboss>
<enterprise-beans>
<session>
<ejb-name>MyBeanName</ejb-name>
<jndi-name>somePath/otherName</jndi-name>
</session>
</enterprise-beans>
</jboss>
</pre><p>(you don't need to specify the rest of the jboss.xml file, only this
information is sufficient in the
new metadata, this is what we call "differential" jboss.xml).</p><p>Your bean is then available in JNDI under somePath/otherName</p><p>In your client code, your call will look like this:</p><pre class="programlisting">import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class Client {
public static void main(String args[]) {
...
InitialContext namingContext = new InitialContext();
Object ref = namingContext.lookup("somePath/otherName");
MyBeanHome home = (MyBeanHome)PortableRemoteObject.narrow(ref, MyBeanHome.class);
...
}
}</pre></div></div><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="432" height="79"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch07s02.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s02.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch07s13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s13.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -