Tomcat 与 Jetty JNDI 查找
我使用 Spring 来配置我的 Java Web 应用程序,并在 Spring 配置中通过 Jetty 的 JNDI 获取数据源,如下所示:
但这不适用于 Tomcat。对于 Tomcat,我必须这样做:
最好的方法是什么解决这个问题吗?我已经使用 JNDI 作为外部化配置的方式,所以我无法外部化我的外部化配置!同时,我绝对讨厌拥有两个单独的 Spring 配置文件的想法。帮助!!!
I use Spring to configure my Java Web App and in my Spring configuration I obtain a datasource via JNDI for Jetty as follows:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myDataSource" />
but this won't work with Tomcat. With Tomcat I have to do this:
<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! At the same time I absolutely loath the idea of having two separate Spring configuration files. HELP!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在这里找到了答案 ,但我认为这有点复杂,但它确实给了我使用非常酷的 ServerDetector 博主找到的类。
一旦我能够动态地确定我正在运行的服务器类型,我就能够使用 Spring 表达式语言来完成其余的工作:
简单!
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.
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:
Easy!
经过一些实验,我发现我可以强制 Jetty 使用与 Tomcat 相同的 JNDI 路径。以下代码片段来自我的
jetty-env.xml
文件:不确定这是否理想,但它有效。
更新:
如果您将 jetty-env.xml 文件放入 WAR 中,它就会起作用...但无论出于何种原因,您将此配置移至 WAR 之外并移至 Jetty 的“上下文”中的上下文片段文件中" 目录,然后抛出异常:
检查一下: http://jira.codehaus.org/browse /JETTY-273
After some experimenting, I figured out I could just force Jetty to use the same JNDI path as Tomcat. The following snippet is from my
jetty-env.xml
file: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:
Check it out: http://jira.codehaus.org/browse/JETTY-273
最简洁的方法是配置您的配置。 ;)
使用 Spring 属性占位符。请参阅
http: //static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer
基本思想是你只需在 spring 中放置一个占位符使用属性进行配置,然后从属性文件中读取匹配的属性。您在构建过程中生成属性文件。我见过这样的情况:构建工具(ant)读取环境变量,然后基于填充了令牌的框架文件创建适合环境的属性文件。
The cleanest way to do it is to configure your configuration. ;)
Use a Spring property place holder. See
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. 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.