Java 如何在 context.xml 中存储字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18404219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store string values in context.xml
提问by
I'd like to store connection URLs in a JNDI binding for my Tomcat application. Since Tomcat uses context.xml
for JNDI resource defining, I need to figure out the propert way to store a String (or multiple strings for multiple connections) in context.xml
.
我想将连接 URL 存储在我的 Tomcat 应用程序的 JNDI 绑定中。由于 Tomcatcontext.xml
用于 JNDI 资源定义,我需要找出在context.xml
.
My reason for doing this is so that I can define different strings for different environments, and load them through JNDI.
我这样做的原因是,我可以为不同的环境定义不同的字符串,并通过 JNDI 加载它们。
Usually, I see entries like so:
通常,我会看到这样的条目:
<Context ...>
<Resource name="someName" auth="Container"
type="someFullyQualifiedClassName"
description="Some description."/>
</Context>
Is it really just as simple as:
真的就这么简单:
<Context ...>
<Resource name="myConnectionURL" auth="Container"
type="java.lang.String"
description="A connection URL string."/>
</Context>
If so, where do I actually store the String value?!?!And if it's not correct, then what is the proper way for me to store, for instance, "amqp:5272//blah.example.com¶m1=4
" in context.xml
so I could then look it up like so:
如果是这样,我实际上在哪里存储字符串值?!?!如果它不正确,那么我存储的正确方法是什么,例如,“ amqp:5272//blah.example.com¶m1=4
”,context.xml
以便我可以这样查找:
Context ctx = new InitialContext();
String connectionURL = (String)ctx.lookup("myConnectionURL");
Thanks in advance!
提前致谢!
采纳答案by Prabhakaran Ramaswamy
You can configure named values that will be made visible to the web application as servlet context initialization parameters by nesting elements inside this element. For example, you can create an initialization parameter like this:
您可以通过在此元素中嵌套元素,将命名值配置为 Web 应用程序可见的 servlet 上下文初始化参数。例如,您可以像这样创建一个初始化参数:
<Context>
...
<Parameter name="companyName" value="My Company, Incorporated"
override="false"/>
...
</Context>
This is equivalent to the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml):
<context-param>
<param-name>companyName</param-name>
<param-value>My Company, Incorporated</param-value>
</context-param>
Your java code looks like this
你的java代码看起来像这样
ServletContext sc = getServletContext();
String companyName = sc.getInitParameter("companyName");
Please see the reference http://tomcat.apache.org/tomcat-7.0-doc/config/context.html
请参阅参考http://tomcat.apache.org/tomcat-7.0-doc/config/context.html
回答by Stoffe
You can use an Environment
tag:
您可以使用Environment
标签:
<Context>
<Environment name="myConnectionURL" value="amqp:5272//blah.example.com¶m1=4" type="java.lang.String"/>
</Context>
And you can read it almost as you specified in the question:
您几乎可以按照问题中的说明阅读它:
InitialContext initialContext = new InitialContext();
Context environmentContext = (Context) initialContext.lookup("java:/comp/env");
String connectionURL = (String) environmentContext.lookup("myConnectionURL");
This is much the same as using a Parameter
tag, but without the need for a ServletContext
.
这与使用Parameter
标签非常相似,但不需要ServletContext
.