關於獲取數據源的語法,大體有(javax.sql.DataSource) ctx.lookup ("java:comp/env/XXX")和(javax.sql.DataSource) ctx.lookup("XXX")兩種寫法,好多人以為這兩種寫法是相同的,以為都是通過JNDI來獲取數據源。其實java:comp/env和 JNDI是不同的,java:comp/env 是環境命名上下文(environment naming context(ENC)),是在EJB規范1.1以后引入的,引入這個是為了解決原來JNDI查找所引起的沖突問題,也是為了提高EJB或者J2EE應用的移植性。ENC是一個引用,引用是用於定位企業應用程序的外部資源的邏輯名。引用是在應用程序部署描述符文件中定義的。在部署時,引用被綁定到目標可操作環境中資源的物理位置(JNDI名)。使用ENC是把對其它資源的JNDI查找的硬編碼解脫出來,通過配置這個引用可以在不修改代碼的情況下,將引用指向不同的EJB(JNDI)。 在J2EE中的引用常用的有:
---------JDBC 數據源引用在java:comp/env/jdbc 子上下文中聲明
---------JMS 連接工廠在java:comp/env/jms 子上下文中聲明
---------JavaMail 連接工廠在java:comp/env/mail 子上下文中聲明
---------URL 連接工廠在 java:comp/env/url子上下文中聲明
假如你寫了一個EJB,獲取datasource如:dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/DBPool");
那么在配置文件中進行資源映射時,在ejb-jar.xml中,
<resource-ref>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在weblogic-ejb-jar.xml中,
<reference-descriptor>
<resource-description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<jndi-name>OraDataSource</jndi-name>
</resource-description>
</reference-descriptor>
//轉者注:如果是在jboss則在jboss.xml中做如下修改
<resource-managers>
<resource-manager>
<res-name>jdbc/DBPool</res-name>
<res-jndi-name>OraDataSource</res-jndi-name>
</resource-manager>
</resource-managers>
實際服務器中的JNDI名字是OraDataSource,邏輯名jdbc/DBPool只是用來和它作映射的,這樣做的好處是為了提高可移植性,移植的時候只需要把配置文件改一下就可以,而應用程序可不用改動。
假如你寫了一個一般的應用程序,想直接通過JNDI來獲取數據源,那么直接lookup(“mytest”)就可以了(假如服務器上的JNDI為mytest),用第一種寫法反而會報錯的。
---------JDBC 數據源引用在java:comp/env/jdbc 子上下文中聲明
---------JMS 連接工廠在java:comp/env/jms 子上下文中聲明
---------JavaMail 連接工廠在java:comp/env/mail 子上下文中聲明
---------URL 連接工廠在 java:comp/env/url子上下文中聲明
假如你寫了一個EJB,獲取datasource如:dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/DBPool");
那么在配置文件中進行資源映射時,在ejb-jar.xml中,
<resource-ref>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在weblogic-ejb-jar.xml中,
<reference-descriptor>
<resource-description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<jndi-name>OraDataSource</jndi-name>
</resource-description>
</reference-descriptor>
//轉者注:如果是在jboss則在jboss.xml中做如下修改
<resource-managers>
<resource-manager>
<res-name>jdbc/DBPool</res-name>
<res-jndi-name>OraDataSource</res-jndi-name>
</resource-manager>
</resource-managers>
實際服務器中的JNDI名字是OraDataSource,邏輯名jdbc/DBPool只是用來和它作映射的,這樣做的好處是為了提高可移植性,移植的時候只需要把配置文件改一下就可以,而應用程序可不用改動。
假如你寫了一個一般的應用程序,想直接通過JNDI來獲取數據源,那么直接lookup(“mytest”)就可以了(假如服務器上的JNDI為mytest),用第一種寫法反而會報錯的。
轉自:http://www.blogjava.net/javababy/archive/2006/12/06/85881.html
----------------------------------------------------------------------------------------------------------------------------------------------
ENC的概念:
The application component environment is referred to as the ENC, the enterprise naming context.
應用組件的業務邏輯應該是ENC中取得對象。組件提供者使用標准的部署描述符指定必需的ENC入口。這些ENC入口是運行時組件所依賴的資源等信息。
一個應用組件實例使用JNDI定位ENC。ENC的標准JNDI CONTEXT是:java;/comp/env
env環境是一個私有的環境,只能在該組件內部存取。對其它組件是不可見的。比如,EJB1不能存取EJB2的ENV。同樣,任何一個客戶端代碼,不管它是運行在應用服務器的同一個JVM內或者是遠程調用,均不能訪問這些JNDI。這樣的話,組件之間是相互隔離的,不同組件之間可以定義它自己的環境參數。比如EJB1可以定義自己的環境變量參數:java:comp/env/red
1、JBOSS的命名空間:
比如一個命名: java:comp/env 凡是綁定在java: 名字下面的子環境,均只能在JBOSS 服務器內部才能進行訪問。無法進行遠程訪問,比如DataSource 均被綁定在 java:下面。不能遠程訪問。而EJB,JTA均被綁定在全局范圍之內,能進行遠程訪問。
一些慣用的ENV:
ENV 用env-entry 進行申明。
ejb引用用 ejb-ref,ejb-local-ref 進行申明。
資源管理連接工廠用resource-ref 進行申明。
資源環境變量引用用resource-env-ref進行申明。
1.1 Environment Entries
例子:
ENC env-entry access code fragment
1.2 EJB Reference
在開發過程,有時候需在EJB之間進行相互引用。這個引用一般均要根據JNDI名字才能進行,但JNDI名字是在部署時才能決定。因此需要一種手段,引用在部署時的另一個EJB,ejb-reference就是為了滿足這種要求。
ejb reference 是一個在應用組件命名環境下指向一個部署時EJB HOME的鏈接。J2EE規范推薦所有的此種鏈接應接被組織在java:/comp/env/ejb命名空間下。
ejb-ref的范圍是屬於申明此段屬性的EJB組件。這就意味着其它的應用組件在運行時不能訪問這些屬性。其它組件定義相同的名字也不會引用名稱沖突。
JBOSS的引用方法:
從上面可以看出,如果ejb/ProductHome的引用被重新定向給了jndi-name : jboss/store/ProductHome
1.3 EJB Local Reference
EJB 2.0 增加了 ejb local reference , 從而從語義學的角度保證該引用為本地引用,而不是通過RMI的方式。
存取的語法:
1.4 Resource manaager connection factory reference
由一系列的resource-ref 組成。每個引用以下幾個子節點:
description (optional)
res-ref-name : java:/comp/env 下面的相關的名字
res-type : 資源的類型,資源管理器連接工廠的全限定名稱。
res-auth : 資源需要的權限管理。 分兩種:Application或 container
res-share-scope (optional) : 目前JBOSS不支持。
J2EE規范推薦所有資源引用都組織在java:/comp/env 名稱空間之下,並且每類資源有自己的子命名空間。
比如:JDBC DataSource Reference should be declared in the java:/comp/env/jdbc subContext.
JMS connection factories should be declared int the java:/comp/env/jms subContext.
一個在WEB.XML文件中的引用例子:
程序中進行引用:
jboss-web.xml(對應於WEB),jboss.xml(對應於ejb) 文件提供res-ref-name對JNDI名字的轉換。
這是通過在jboss.xml 或 jboss-web.xml 文件中提供 resource-ref 完成的。
比如:
Resource Environment References 提供了 resource 本身需要使用的資源。
在程序中訪問:
同樣道理,特定平台仍需提供 resource-env-ref-name 至 JNDI 的轉換,比如在JBOSS.xml文件中:
轉自:http://lym6520.javaeye.com/blog/316169
The application component environment is referred to as the ENC, the enterprise naming context.
應用組件的業務邏輯應該是ENC中取得對象。組件提供者使用標准的部署描述符指定必需的ENC入口。這些ENC入口是運行時組件所依賴的資源等信息。
一個應用組件實例使用JNDI定位ENC。ENC的標准JNDI CONTEXT是:java;/comp/env
- // Obtain the application component's ENC
- Context iniCtx = new InitialContext();
- Context compEnv = (Context) iniCtx.lookup( "java:comp/env" );
- // Obtain the application component's ENC
- Context iniCtx = new InitialContext();
- Context compEnv = (Context) iniCtx.lookup("java:comp/env");
env環境是一個私有的環境,只能在該組件內部存取。對其它組件是不可見的。比如,EJB1不能存取EJB2的ENV。同樣,任何一個客戶端代碼,不管它是運行在應用服務器的同一個JVM內或者是遠程調用,均不能訪問這些JNDI。這樣的話,組件之間是相互隔離的,不同組件之間可以定義它自己的環境參數。比如EJB1可以定義自己的環境變量參數:java:comp/env/red
1、JBOSS的命名空間:
比如一個命名: java:comp/env 凡是綁定在java: 名字下面的子環境,均只能在JBOSS 服務器內部才能進行訪問。無法進行遠程訪問,比如DataSource 均被綁定在 java:下面。不能遠程訪問。而EJB,JTA均被綁定在全局范圍之內,能進行遠程訪問。
一些慣用的ENV:
ENV 用env-entry 進行申明。
ejb引用用 ejb-ref,ejb-local-ref 進行申明。
資源管理連接工廠用resource-ref 進行申明。
資源環境變量引用用resource-env-ref進行申明。
1.1 Environment Entries
例子:
- < session >
- < ejb-name > ASessionBean </ ejb-name >
- <!-- ... -->
- < env-entry >
- < description > The maximum number of tax exemptions allowed </ description >
- < env-entry-name > maxExemptions </ env-entry-name >
- < env-entry-type > java.lang.Integer </ env-entry-type >
- < env-entry-value > 15 </ env-entry-value >
- </ env-entry >
- < env-entry >
- < description > The tax rate </ description >
- < env-entry-name > taxRate </ env-entry-name >
- < env-entry-type > java.lang.Float </ env-entry-type >
- < env-entry-value > 0.23 </ env-entry-value >
- </ env-entry >
- </ session >
- <session>
- <ejb-name>ASessionBean</ejb-name>
- <!-- ... -->
- <env-entry>
- <description>The maximum number of tax exemptions allowed </description>
- <env-entry-name>maxExemptions</env-entry-name>
- <env-entry-type>java.lang.Integer</env-entry-type>
- <env-entry-value>15</env-entry-value>
- </env-entry>
- <env-entry>
- <description>The tax rate </description>
- <env-entry-name>taxRate</env-entry-name>
- <env-entry-type>java.lang.Float</env-entry-type>
- <env-entry-value>0.23</env-entry-value>
- </env-entry>
- </session>
ENC env-entry access code fragment
- InitialContext iniCtx = new InitialContext();
- Context envCtx = (Context) iniCtx.lookup( "java:comp/env" );
- Integer maxExemptions = (Integer) envCtx.lookup( "maxExemptions" );
- Float taxRate = (Float) envCtx.lookup( "taxRate" );
- InitialContext iniCtx = new InitialContext();
- Context envCtx = (Context) iniCtx.lookup("java:comp/env");
- Integer maxExemptions = (Integer) envCtx.lookup("maxExemptions");
- Float taxRate = (Float) envCtx.lookup("taxRate");
1.2 EJB Reference
在開發過程,有時候需在EJB之間進行相互引用。這個引用一般均要根據JNDI名字才能進行,但JNDI名字是在部署時才能決定。因此需要一種手段,引用在部署時的另一個EJB,ejb-reference就是為了滿足這種要求。
ejb reference 是一個在應用組件命名環境下指向一個部署時EJB HOME的鏈接。J2EE規范推薦所有的此種鏈接應接被組織在java:/comp/env/ejb命名空間下。
- < session >
- < ejb-name > ShoppingCartBean </ ejb-name >
- <!-- ...-->
- </ session >
- < session >
- < ejb-name > ProductBeanUser </ ejb-name >
- <!--...-->
- < ejb-ref >
- < description > This is a reference to the store products entity </ description > 此屬性可選
- < ejb-ref-name > ejb/ProductHome </ ejb-ref-name >
- < ejb-ref-type > Entity </ ejb-ref-type > 只能選Entity與Session
- < home > org.jboss.store.ejb.ProductHome </ home >
- </ ejb-ref >
- < remote > org.jboss.store.ejb.Product </ remote >
- </ session >
- < session >
- < ejb-name > ShoppingCartUser </ ejb-name >
- <!--...-->
- < ejb-ref >
- < ejb-ref-name > ejb/ShoppingCartHome </ ejb-ref-name >
- < ejb-ref-type > Session </ ejb-ref-type >
- < home > org.jboss.store.ejb.ShoppingCartHome </ home >
- < remote > org.jboss.store.ejb.ShoppingCart </ remote >
- < ejb-link > ShoppingCartBean </ ejb-link >
- <!--指向同一個EJB JAR 或 同一個 APPLICATION UNIT中的一個EJB,且是該 EJB的名字。-->
- </ ejb-ref >
- </ session >
- < entity >
- < description > The Product entity bean </ description >
- < ejb-name > ProductBean </ ejb-name >
- <!--...-->
- </ entity >
- <session>
- <ejb-name>ShoppingCartBean</ejb-name>
- <!-- ...-->
- </session>
- <session>
- <ejb-name>ProductBeanUser</ejb-name>
- <!--...-->
- <ejb-ref>
- <description>This is a reference to the store products entity </description> 此屬性可選
- <ejb-ref-name>ejb/ProductHome</ejb-ref-name>
- <ejb-ref-type>Entity</ejb-ref-type> 只能選Entity與Session
- <home>org.jboss.store.ejb.ProductHome</home>
- </ejb-ref>
- <remote> org.jboss.store.ejb.Product</remote>
- </session>
- <session>
- <ejb-name>ShoppingCartUser</ejb-name>
- <!--...-->
- <ejb-ref>
- <ejb-ref-name>ejb/ShoppingCartHome</ejb-ref-name>
- <ejb-ref-type>Session</ejb-ref-type>
- <home>org.jboss.store.ejb.ShoppingCartHome</home>
- <remote> org.jboss.store.ejb.ShoppingCart</remote>
- <ejb-link>ShoppingCartBean</ejb-link>
- <!--指向同一個EJB JAR 或 同一個 APPLICATION UNIT中的一個EJB,且是該 EJB的名字。-->
- </ejb-ref>
- </session>
- <entity>
- <description>The Product entity bean </description>
- <ejb-name>ProductBean</ejb-name>
- <!--...-->
- </entity>
ejb-ref的范圍是屬於申明此段屬性的EJB組件。這就意味着其它的應用組件在運行時不能訪問這些屬性。其它組件定義相同的名字也不會引用名稱沖突。
- InitialContext iniCtx = new InitialContext();
- Context ejbCtx = (Context) iniCtx.lookup( "java:comp/env/ejb" );
- ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup( "ShoppingCartHome" );
- InitialContext iniCtx = new InitialContext();
- Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
- ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup("ShoppingCartHome");
JBOSS的引用方法:
- < session >
- < ejb-name > ProductBeanUser </ ejb-name >
- < ejb-ref >
- < ejb-ref-name > ejb/ProductHome </ ejb-ref-name > 對應於ejb-jar.xml文件中的ejb-ref-name元素
- < jndi-name > jboss/store/ProductHome </ jndi-name >
- </ ejb-ref >
- </ session >
- < entity >
- < ejb-name > ProductBean </ ejb-name >
- < jndi-name > jboss/store/ProductHome </ jndi-name >
- <!-- ... -->
- </ entity >
- <session>
- <ejb-name>ProductBeanUser</ejb-name>
- <ejb-ref>
- <ejb-ref-name>ejb/ProductHome</ejb-ref-name> 對應於ejb-jar.xml文件中的ejb-ref-name元素
- <jndi-name>jboss/store/ProductHome</jndi-name>
- </ejb-ref>
- </session>
- <entity>
- <ejb-name>ProductBean</ejb-name>
- <jndi-name>jboss/store/ProductHome</jndi-name>
- <!-- ... -->
- </entity>
從上面可以看出,如果ejb/ProductHome的引用被重新定向給了jndi-name : jboss/store/ProductHome
1.3 EJB Local Reference
EJB 2.0 增加了 ejb local reference , 從而從語義學的角度保證該引用為本地引用,而不是通過RMI的方式。
- < session >
- < ejb-name > Probe </ ejb-name >
- < home > org.jboss.test.perf.interfaces.ProbeHome </ home >
- < remote > org.jboss.test.perf.interfaces.Probe </ remote >
- < local-home > org.jboss.test.perf.interfaces.ProbeLocalHome </ local-home >
- < local > org.jboss.test.perf.interfaces.ProbeLocal </ local >
- < ejb-class > org.jboss.test.perf.ejb.ProbeBean </ ejb-class >
- < session-type > Stateless </ session-type >
- < transaction-type > Bean </ transaction-type >
- </ session >
- < session >
- < ejb-name > PerfTestSession </ ejb-name >
- < home > org.jboss.test.perf.interfaces.PerfTestSessionHome </ home >
- < remote > org.jboss.test.perf.interfaces.PerfTestSession </ remote >
- < ejb-class > org.jboss.test.perf.ejb.PerfTestSessionBean </ ejb-class >
- < session-type > Stateless </ session-type >
- < transaction-type > Container </ transaction-type >
- < ejb-ref >
- < ejb-ref-name > ejb/ProbeHome </ ejb-ref-name >
- < ejb-ref-type > Session </ ejb-ref-type >
- < home > org.jboss.test.perf.interfaces.SessionHome </ home >
- < remote > org.jboss.test.perf.interfaces.Session </ remote >
- < ejb-link > Probe </ ejb-link >
- </ ejb-ref >
- < ejb-local-ref >
- < ejb-ref-name > ejb/ProbeLocalHome </ ejb-ref-name >
- < ejb-ref-type > Session </ ejb-ref-type >
- < local-home > org.jboss.test.perf.interfaces.ProbeLocalHome </ local-home >
- < local > org.jboss.test.perf.interfaces.ProbeLocal </ local >
- <!--EJB local interface 的全限定名-->
- < ejb-link > Probe </ ejb-link >
- </ ejb-local-ref >
- </ session >
- <session>
- <ejb-name>Probe</ejb-name>
- <home>org.jboss.test.perf.interfaces.ProbeHome</home>
- <remote>org.jboss.test.perf.interfaces.Probe</remote>
- <local-home>org.jboss.test.perf.interfaces.ProbeLocalHome</local-home>
- <local>org.jboss.test.perf.interfaces.ProbeLocal</local>
- <ejb-class>org.jboss.test.perf.ejb.ProbeBean</ejb-class>
- <session-type>Stateless</session-type>
- <transaction-type>Bean</transaction-type>
- </session>
- <session>
- <ejb-name>PerfTestSession</ejb-name>
- <home>org.jboss.test.perf.interfaces.PerfTestSessionHome</home>
- <remote>org.jboss.test.perf.interfaces.PerfTestSession</remote>
- <ejb-class>org.jboss.test.perf.ejb.PerfTestSessionBean</ejb-class>
- <session-type>Stateless</session-type>
- <transaction-type>Container</transaction-type>
- <ejb-ref>
- <ejb-ref-name>ejb/ProbeHome</ejb-ref-name>
- <ejb-ref-type>Session</ejb-ref-type>
- <home>org.jboss.test.perf.interfaces.SessionHome</home>
- <remote>org.jboss.test.perf.interfaces.Session</remote>
- <ejb-link>Probe</ejb-link>
- </ejb-ref>
- <ejb-local-ref>
- <ejb-ref-name>ejb/ProbeLocalHome</ejb-ref-name>
- <ejb-ref-type>Session</ejb-ref-type>
- <local-home>org.jboss.test.perf.interfaces.ProbeLocalHome</local-home>
- <local>org.jboss.test.perf.interfaces.ProbeLocal</local>
- <!--EJB local interface 的全限定名-->
- <ejb-link>Probe</ejb-link>
- </ejb-local-ref>
- </session>
存取的語法:
- InitialContext iniCtx = new InitialContext();
- Context ejbCtx = (Context) iniCtx.lookup( "java:comp/env/ejb" );
- ProbeLocalHome home = (ProbeLocalHome) ejbCtx.lookup( "ProbeLocalHome" );
- InitialContext iniCtx = new InitialContext();
- Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
- ProbeLocalHome home = (ProbeLocalHome) ejbCtx.lookup("ProbeLocalHome");
1.4 Resource manaager connection factory reference
由一系列的resource-ref 組成。每個引用以下幾個子節點:
description (optional)
res-ref-name : java:/comp/env 下面的相關的名字
res-type : 資源的類型,資源管理器連接工廠的全限定名稱。
res-auth : 資源需要的權限管理。 分兩種:Application或 container
res-share-scope (optional) : 目前JBOSS不支持。
J2EE規范推薦所有資源引用都組織在java:/comp/env 名稱空間之下,並且每類資源有自己的子命名空間。
比如:JDBC DataSource Reference should be declared in the java:/comp/env/jdbc subContext.
JMS connection factories should be declared int the java:/comp/env/jms subContext.
一個在WEB.XML文件中的引用例子:
- < web >
- <!-- ... -->
- < servlet >
- < servlet-name > AServlet </ servlet-name >
- <!-- ... -->
- </ servlet >
- <!-- ... -->
- <!-- JDBC DataSources (java:comp/env/jdbc) -->
- < resource-ref >
- < description > The default DS </ description >
- < res-ref-name > jdbc/DefaultDS </ res-ref-name > <!--該名稱必須與下面的描述文件中的一致-->
- < res-type > javax.sql.DataSource </ res-type >
- < res-auth > Container </ res-auth >
- </ resource-ref >
- <!-- JavaMail Connection Factories (java:comp/env/mail) -->
- < resource-ref >
- < description > Default Mail </ description >
- < res-ref-name > mail/DefaultMail </ res-ref-name >
- < res-type > javax.mail.Session </ res-type >
- < res-auth > Container </ res-auth >
- </ resource-ref >
- <!-- JMS Connection Factories (java:comp/env/jms) -->
- < resource-ref >
- < description > Default QueueFactory </ description >
- < res-ref-name > jms/QueueFactory </ res-ref-name >
- < res-type > javax.jms.QueueConnectionFactory </ res-type >
- < res-auth > Container </ res-auth >
- </ resource-re >
- <web>
- <!-- ... -->
- <servlet>
- <servlet-name>AServlet</servlet-name>
- <!-- ... -->
- </servlet>
- <!-- ... -->
- <!-- JDBC DataSources (java:comp/env/jdbc) -->
- <resource-ref>
- <description>The default DS</description>
- <res-ref-name>jdbc/DefaultDS</res-ref-name> <!--該名稱必須與下面的描述文件中的一致-->
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- <!-- JavaMail Connection Factories (java:comp/env/mail) -->
- <resource-ref>
- <description>Default Mail</description>
- <res-ref-name>mail/DefaultMail</res-ref-name>
- <res-type>javax.mail.Session</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- <!-- JMS Connection Factories (java:comp/env/jms) -->
- <resource-ref>
- <description>Default QueueFactory</description>
- <res-ref-name>jms/QueueFactory</res-ref-name>
- <res-type>javax.jms.QueueConnectionFactory</res-type>
- <res-auth>Container</res-auth>
- </resource-re>
程序中進行引用:
- Context initCtx = new InitialContext();
- javax.mail.Session s = (javax.mail.Session)
- initCtx.lookup( "java:comp/env/mail/DefaultMail" );
- Context initCtx = new InitialContext();
- javax.mail.Session s = (javax.mail.Session)
- initCtx.lookup("java:comp/env/mail/DefaultMail");
jboss-web.xml(對應於WEB),jboss.xml(對應於ejb) 文件提供res-ref-name對JNDI名字的轉換。
這是通過在jboss.xml 或 jboss-web.xml 文件中提供 resource-ref 完成的。
比如:
- < jboss-web >
- <!-- ... -->
- < resource-ref >
- < res-ref-name > jdbc/DefaultDS </ res-ref-name >
- < res-type > javax.sql.DataSource </ res-type >
- < jndi-name > java:/DefaultDS </ jndi-name > <!--將資源引用重定向到JNDI上面-->
- </ resource-ref >
- < resource-ref >
- < res-ref-name > mail/DefaultMail </ res-ref-name >
- < res-type > javax.mail.Session </ res-type >
- < jndi-name > java:/Mail </ jndi-name >
- </ resource-ref >
- < resource-ref >
- < res-ref-name > jms/QueueFactory </ res-ref-name >
- < res-type > javax.jms.QueueConnectionFactory </ res-type >
- < jndi-name > QueueConnectionFactory </ jndi-name >
- </ resource-ref >
- <!-- ... -->
- </ jboss-web >
- <jboss-web>
- <!-- ... -->
- <resource-ref>
- <res-ref-name>jdbc/DefaultDS</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <jndi-name>java:/DefaultDS</jndi-name> <!--將資源引用重定向到JNDI上面-->
- </resource-ref>
- <resource-ref>
- <res-ref-name>mail/DefaultMail</res-ref-name>
- <res-type>javax.mail.Session</res-type>
- <jndi-name>java:/Mail</jndi-name>
- </resource-ref>
- <resource-ref>
- <res-ref-name>jms/QueueFactory</res-ref-name>
- <res-type>javax.jms.QueueConnectionFactory</res-type>
- <jndi-name>QueueConnectionFactory</jndi-name>
- </resource-ref>
- <!-- ... -->
- </jboss-web>
Resource Environment References 提供了 resource 本身需要使用的資源。
- < session >
- < ejb-name > MyBean </ ejb-name >
- < resource-env-ref >
- < description > This is a reference to a JMS queue used in the
- processing of Stock info
- </ description >
- < resource-env-ref-name > jms/StockInfo </ resource-env-ref-name >
- < resource-env-ref-type > javax.jms.Queue </ resource-env-ref-type >
- </ resource-env-ref >
- <!-- ... -->
- </ session >
- <session>
- <ejb-name>MyBean</ejb-name>
- <resource-env-ref>
- <description>This is a reference to a JMS queue used in the
- processing of Stock info
- </description>
- <resource-env-ref-name>jms/StockInfo</resource-env-ref-name>
- <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
- </resource-env-ref>
- <!-- ... -->
- </session>
在程序中訪問:
- InitialContext iniCtx = new InitialContext();
- javax.jms.Queue q = (javax.jms.Queue)
- envCtx.lookup( "java:comp/env/jms/StockInfo" );
- InitialContext iniCtx = new InitialContext();
- javax.jms.Queue q = (javax.jms.Queue)
- envCtx.lookup("java:comp/env/jms/StockInfo");
同樣道理,特定平台仍需提供 resource-env-ref-name 至 JNDI 的轉換,比如在JBOSS.xml文件中:
- < session >
- < ejb-name > MyBean </ ejb-name >
- < resource-env-ref >
- < resource-env-ref-name > jms/StockInfo </ resource-env-ref-name >
- < jndi-name > queue/StockInfoQueue </ jndi-name > 此為真正的全局JNDI名字
- </ resource-env-ref >
- <!-- ... -->
- </ session >
- <session>
- <ejb-name>MyBean</ejb-name>
- <resource-env-ref>
- <resource-env-ref-name>jms/StockInfo</resource-env-ref-name>
- <jndi-name>queue/StockInfoQueue</jndi-name> 此為真正的全局JNDI名字
- </resource-env-ref>
- <!-- ... -->
- </session>