简体   繁体   English

Tomcat与Jetty JNDI Lookup

[英]Tomcat vs Jetty JNDI Lookup

I use Spring to configure my Java Web App and in my Spring configuration I obtain a datasource via JNDI for Jetty as follows: 我使用Spring配置我的Java Web App,在Spring配置中,我通过JNDI为Jetty获取数据源,如下所示:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myDataSource" />

but this won't work with Tomcat. 但这不适用于Tomcat。 With Tomcat I have to do this: 使用Tomcat我必须这样做:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myDataSource" />

Whats the best way to solve this? 什么是解决这个问题的最佳方法? I am already using JNDI as a way to externalize configuration, so I can't externalize my externalized configuration! 我已经在使用JNDI作为外部化配置的方法,所以我不能外化我的外部化配置! At the same time I absolutely loath the idea of having two separate Spring configuration files. 同时我完全不喜欢有两个单独的Spring配置文件。 HELP!!! 救命!!!

I found an answer here , but I thought it was a bit complicated, but it did give me the idea to use the very cool ServerDetector class that blogger had found. 我在这里找到了答案,但我认为它有点复杂,但它确实让我想到使用博客发现的非常酷的ServerDetector类。

Once I can dynamically figure what type of server I am running in, I was able to use the Spring expression language to do the rest of the work: 一旦我可以动态地计算出我正在运行的服务器类型,我就可以使用Spring表达式语言来完成剩下的工作:

<jee:jndi-lookup id="myAppDataSource" 
    jndi-name="#{ (AppServerType == 'Jetty' ? 'jdbc/' : 'java:comp/env/jdbc/') + 
                  'myAppDataSource' }" />

Easy! 简单!

After some experimenting, I figured out I could just force Jetty to use the same JNDI path as Tomcat. 经过一些实验,我发现我可以强制Jetty使用与Tomcat相同的JNDI路径。 The following snippet is from my jetty-env.xml file: 以下代码段来自我的jetty-env.xml文件:

 <New id="myDataSource" class="org.mortbay.jetty.plus.naming.Resource">
  <!-- We MUST specify the entire JNDI path here to force compliance with the Tomcat/J2EE convention -->
  <Arg>java:comp/env/jdbc/myDataSource</Arg>
  <Arg>
   <New class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
    <Set name="uniqueResourceName">sbeDatabase</Set>
                 ...............
   </New>
  </Arg>
 </New>

Not sure if this is ideal, but it works. 不确定这是否理想,但它确实有效。

Update : 更新

It works if you put your jetty-env.xml file inside the WAR...but for whatever reason, one you move this configuration outside the WAR and into a context fragment file in Jetty's "contexts" directory then it throws an exception: 如果你将你的jetty-env.xml文件放在WAR中,它就可以工作......但无论出于何种原因,你可以将这个配置移到WAR之外并放入Jetty的“contexts”目录中的上下文片段文件中,然后抛出异常:

Check it out: http://jira.codehaus.org/browse/JETTY-273 看看: http//jira.codehaus.org/browse/JETTY-273

The cleanest way to do it is to configure your configuration. 最简单的方法是配置您的配置。 ;) ;)

Use a Spring property place holder. 使用Spring属性占位符。 See 看到

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer

The basic idea is that you just put a placeholder in your spring config with a property, and then it reads matching property from a properties file. 基本思想是你只需在spring配置中放置一个带有属性的占位符,然后从属性文件中读取匹配的属性。 You generate the properties file in your build process. 您在构建过程中生成属性文件。 Ive seen it done where the build tool (ant) reads an environment variable and then creates a properties file appropriate for the environment based of a skeleton file populated with tokens. 我已经看到它在构建工具(ant)读取环境变量的情况下完成,然后根据填充了标记的框架文件创建适合环境的属性文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM