前面的Hello.java程序中有这么一段:
Properties environment = new Properties();
environment.put(Context.PROVIDER_URL, "localhost:1099");
environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
.....
initialContext = new InitialContext(environment);
按照我的理解这段程序相当于从"localhost"这个容器中取得当前的环境变量.既然是本地的为什么还要把environment参数传进去呢.
直接initialContext = new InitialContext(); 不行吗?其实是可以的,只要修改ejb-jar.xml:
<!---->
<!---->http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar></ejb-jar>
<description></description>
<display-name></display-name>Generated by XDoclet
<enterprise-beans></enterprise-beans>
<!---->
<session></session>
<description></description>
<display-name></display-name>Name for Hello
<ejb-name></ejb-name>HelloWorld
<home></home>com.rox.HelloHome
<remote></remote>com.rox.HelloWorld
<ejb-class></ejb-class>com.rox.Hello
<session-type></session-type>Stateless
<transaction-type> </transaction-type> Container
<ejb-ref></ejb-ref>
<ejb-ref-name></ejb-ref-name>com.rox.HelloEntityHome
<ejb-ref-type></ejb-ref-type>Entity
<home></home>com.rox.HelloEntityHome
<remote></remote>com.rox.HelloEntityInterface
<ejb-link></ejb-link>HelloEntityInterface
<!---->
<!---->
<entity></entity>
<description></description>
<display-name></display-name>Name for HelloEntity
<ejb-name></ejb-name>HelloEntityInterface
<home></home>com.rox.HelloEntityHome
<remote></remote>com.rox.HelloEntityInterface
<ejb-class></ejb-class>com.rox.HelloEntity
<persistence-type></persistence-type> Container
<prim-key-class></prim-key-class> java.lang.Integer
<reentrant></reentrant>False
<cmp-version></cmp-version>2.x
<abstract-schema-name></abstract-schema-name>HelloEntity
<cmp-field></cmp-field>
<field-name></field-name>helloNo
<cmp-field></cmp-field>
<field-name></field-name>helloName
<primkey-field></primkey-field> helloNo
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<assembly-descriptor></assembly-descriptor>
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
注意红色的内容,那就是问题的关键.至于其中的原理,我仍在思考.
改变了ejb-jar.xml相应的需要修改Hello.java:
将原来的:
Object obj = initialContext.lookup("ejb/HelloEntity");
替换为:
Object obj = initialContext.lookup("java:comp/env/"+HelloEntityHome.class.getName());
按照我的理解comp是容器,env是环境,意思是从当前环境中lookup "HelloEntityHome.class.getName()".