`
gudujianxuehu
  • 浏览: 95873 次
  • 来自: ...
社区版块
存档分类
最新评论

java:comp/env 解释

 
阅读更多
     关于获取数据源的语法,大体有(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),用第一种写法反而会报错的。


转自: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

// 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 >  


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" );   


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 >


ejb-ref的范围是属于申明此段属性的EJB组件。这就意味着其它的应用组件在运行时不能访问这些属性。其它组件定义相同的名字也不会引用名称冲突。
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 > 


从上面可以看出,如果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 > 


存取的语法:

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文件中的引用例子:
01.< web >   
02.     <!-- ... -->   
03.     < servlet >   
04.         < servlet-name > AServlet </ servlet-name >   
05.         <!-- ... -->   
06.     </ servlet >   
07.     <!-- ... -->   
08.     <!-- JDBC DataSources (java:comp/env/jdbc) -->   
09.     < resource-ref >   
10.         < description > The default DS </ description >   
11.         < res-ref-name > jdbc/DefaultDS </ res-ref-name >   <!--该名称必须与下面的描述文件中的一致-->   
12.         < res-type > javax.sql.DataSource </ res-type >   
13.         < res-auth > Container </ res-auth >   
14.     </ resource-ref >   
15.     <!-- JavaMail Connection Factories (java:comp/env/mail) -->   
16.     < resource-ref >   
17.         < description > Default Mail </ description >   
18.         < res-ref-name > mail/DefaultMail </ res-ref-name >   
19.         < res-type > javax.mail.Session </ res-type >   
20.         < res-auth > Container </ res-auth >   
21.     </ resource-ref >   
22.     <!-- JMS Connection Factories (java:comp/env/jms) -->   
23.     < resource-ref >   
24.         < description > Default QueueFactory </ description >   
25.         < res-ref-name > jms/QueueFactory </ res-ref-name >   
26.         < res-type > javax.jms.QueueConnectionFactory </ res-type >   
27.         < res-auth > Container </ res-auth >   
28.     </ resource-re >


程序中进行引用:
01.Context initCtx =  new  InitialContext();   
02.javax.mail.Session s = (javax.mail.Session)   
03.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 完成的。

比如:
01.< jboss-web >   
02.     <!-- ... -->   
03.     < resource-ref >   
04.         < res-ref-name > jdbc/DefaultDS </ res-ref-name >   
05.         < res-type > javax.sql.DataSource </ res-type >   
06.         < jndi-name > java:/DefaultDS </ jndi-name >    <!--将资源引用重定向到JNDI上面-->   
07.     </ resource-ref >   
08.     < resource-ref >   
09.         < res-ref-name > mail/DefaultMail </ res-ref-name >   
10.         < res-type > javax.mail.Session </ res-type >   
11.         < jndi-name > java:/Mail </ jndi-name >   
12.     </ resource-ref >   
13.     < resource-ref >   
14.         < res-ref-name > jms/QueueFactory </ res-ref-name >   
15.         < res-type > javax.jms.QueueConnectionFactory </ res-type >   
16.         < jndi-name > QueueConnectionFactory </ jndi-name >   
17.     </ resource-ref >   
18.     <!-- ... -->   
19.</ jboss-web >  


Resource Environment References 提供了 resource 本身需要使用的资源。

01.< session >   
02.     < ejb-name > MyBean </ ejb-name >   
03.       
04.     < resource-env-ref >   
05.         < description > This is a reference to a JMS queue used in the   
06.            processing of Stock info   
07.         </ description >   
08.         < resource-env-ref-name > jms/StockInfo </ resource-env-ref-name >   
09.         < resource-env-ref-type > javax.jms.Queue </ resource-env-ref-type >   
10.     </ resource-env-ref >   
11.     <!-- ... -->   
12.</ session > 


在程序中访问:

01.InitialContext iniCtx =  new  InitialContext();   
02.javax.jms.Queue q = (javax.jms.Queue)   
03.envCtx.lookup( "java:comp/env/jms/StockInfo" );


同样道理,特定平台仍需提供 resource-env-ref-name 至 JNDI 的转换,比如在JBOSS.xml文件中

01.< session >   
02.     < ejb-name > MyBean </ ejb-name >            
03.         < resource-env-ref >   
04.         < resource-env-ref-name > jms/StockInfo </ resource-env-ref-name >   
05.         < jndi-name > queue/StockInfoQueue </ jndi-name >    此为真正的全局JNDI名字   
06.     </ resource-env-ref >   
07.     <!-- ... -->   
08.</ session >  






分享到:
评论

相关推荐

    HashMap关系数据映射技术软件jadepool-1.1-GBK

    本次修订内容:1、修订了ProcessVO类中java....3、去掉DbConnection早期数据源名称需要加"java:comp/env/"前缀的做法,修订后,是否需要加此前缀,请用户根据需要决定。已在Tomcat、GlassFish中测试通过数据源连接。

    Java Jndi数据连接池,sql2000或者sql2005数据库

    DataSource source = (DataSource) ic.lookup("java:comp/env/jdbc/jndidemo"); dbcon = source.getConnection(); 需要导的包 import javax.naming.Context; import javax.naming.InitialContext; import ...

    chenyeshen#docisfy_blog#SpringBoo配置文件加载1

    1、命令行参数 2、来自java:comp/env的JNDI属性 3、Java系统属性(System.getProperties()) 4、操作系统环境变量 5

    JNDI连接数据库配置

    Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/DevDB"); Connection conn = ds.getConnection(); pw.write("Cool,Successful!&lt;/br&gt;"+conn....

    Tomcat统一配置C3P0连接池

    &lt;!-- 新的数据源 --&gt; &lt;value&gt;java:comp/env/jdbc/commonDB&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt;

    “E-家园”个人网站

    “E-家园”个人网站 ... lookup("java:comp/env/jdbc/ownHome"); conn = source.getConnection(); } catch (Exception e) { throw new DBAccessException("不能取得数据库连接!"); } return conn; }

    IntepowerORM for Java

    ******************关于软件 ***************************************** IntepowerORM是基于Socancode开源代码开发完成的JAVA ORM工具, ... private static final String DRIVER = "java:comp/env/jdbc/MySQLDB";

    jsp连接池成功之例

    } DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); if(ds==null) {throw new Exception("没有匹配数据库"); } con=ds.getConnection(); &lt;br&gt; sql=con....

    spring3.2+strut2+hibernate4

    -- &lt;jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/store" /&gt;--&gt; &lt;!--Hibernate配置--&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;&lt;!...

    java连接数据库工具类--DBConnection(java源码)

    DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndiName); return dataSource.getConnection(); } catch (NamingException ex) { ex.printStackTrace(); } catch ...

    JDBC详解HTML-JDBC.pp

    Context ctx = (Context) new InitialContext().lookup("java:comp/env"); DataSource ds = (DataSource) ctx.lookup(jndi); Connection cn = ds.getConnection(); 3&gt;.执行sql语句(Execute the SQL) &lt;1&gt;.用...

Global site tag (gtag.js) - Google Analytics