InitialContext ctx = new InitialContext();
String jndiName = "java:global/EJBExample/HelloEjbBean!huizhi.HelloEjbBeanLocal";
Object o = ctx.lookup(jndiName);java
獲取到的竟然是HelloEjbBeanLocal$$$View2的proxy代理對象。服務器
這該如何轉換成HelloEjbBeanLocal接口呢?app
原來配置不正確。運維
Jboss7獲取jndi方式與以前版本徹底不同,使用了新技術實現。請看原文說法:dom
Previous versions of JBoss AS (versions < 7.x) used JNP project asthe JNDI naming implementation. Developers of client applications of previousversions of JBoss AS will be familiar with the jnp:// PROVIDER_URL URL theyused to use in their applications for communicating with the JNDI server on theJBoss server.ide
Starting AS7, the JNP project is not used. Neither on the server sidenor on the client side. The client side of the JNP project has now beenreplaced by jboss-remote-naming project. There were various reasons why the JNPclient was replaced by jboss-remote-naming project. One of them was the JNPproject did not allow fine grained security configurations while communicatingwith the JNDI server. The jboss-remote-naming project is backed by thejboss-remoting project which allows much more and better control over security.ui
<subsystem xmlns="urn:jboss:domain:naming:1.1">spa
<bindings>.net
<lookup name="AdminEjb/local" lookup="ejb:/admin_ejb/AdminEjb!com.mipt.admin.ifc.AdminIfc"/>代理
</bindings>
</subsystem>
此方式配置簡單,不用修改代碼,但須要重啓服務器才生效。
主要是配置<lookup/>,name可自定義簡稱,根據name能找到真正的jndi,而後返回對應的ejb實例。
若不須要更改原來項目中獲取方式的代碼,只須要把name改爲對應的AdminEjb/local便可。
Lookup的值爲實際jndi路徑,命名規則以下:
JNDI: ejb:appName /moduleName/distinctName/beanName!viewClassName
appName:這裏是.EAR包的名稱,若是你打包成JAR發佈的話,這裏則留空
moduleName:表示模塊名,也就是ejb包名,但不包括後綴.jar,如admin_ejb.jar。moduleName爲admin_ejb
distinctName:若是沒有定義其更詳細的名稱,則這裏留空
beanName:這裏爲實現類的名稱
viewClassName:爲接口全路徑名稱
缺點:在standalone.xml綁定jndi的話,上線時運維人員需手動添加綁定,使用不方便。
以前接口代碼中通常會把jndi以靜態變量定義,如:adminIfc中的
public static final Java.lang.Stringjndi = 「AdminEjb/local」
如今只須把變量值更改成jndi = 「ejb:/admin_ejb/AdminEjb!com.mipt.admin.ifc.AdminIfc」便可.