java 在哪里/如何为 Tomcat .war 文件设置配置资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1521957/
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
where/how to setup configuration resources for Tomcat .war files
提问by Jason S
I have a source tree for a .war file that I need to modify so that I can add some application-specific configuration info (in this case a jdbc connection string, but I may have other properties-like resources). What are the best practices for where to put configuration info and how to access this from within the Servlet?
我有一个 .war 文件的源树,我需要修改它以便我可以添加一些特定于应用程序的配置信息(在这种情况下是 jdbc 连接字符串,但我可能有其他类似属性的资源)。将配置信息放在哪里以及如何从 Servlet 中访问它的最佳实践是什么?
I'm guessing this Tomcat configuration referencehas something to do with it, but my eyes glaze over when I try to read it.
我猜想这个Tomcat 配置参考与它有关,但是当我试图阅读它时,我的眼睛呆滞了。
采纳答案by Matt Solnit
For the specific case of a JDBC connection string, I would recommend using a Tomcat-managed connection pool instead. You can read more about doing this here: http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
对于 JDBC 连接字符串的特定情况,我建议改用 Tomcat 管理的连接池。您可以在此处阅读有关执行此操作的更多信息:http: //tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
It's more work, but I think in the long run it will serve you better.
这是更多的工作,但我认为从长远来看,它会更好地为您服务。
回答by leonm
For web app configuration you can place the config on the classpath somewhere. Then you can get to it from your application with getResourceAsStreamor if you prefer Spring:
对于 Web 应用程序配置,您可以将配置放在类路径中的某个位置。然后,您可以使用getResourceAsStream从您的应用程序访问它,或者如果您更喜欢 Spring:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:my-config.properties</value>
</list>
</property>
</bean>
There are a number of places you can put the properties on the classpath in Tomcat. in order it looks at:
您可以在 Tomcat 的类路径上放置属性的位置有很多。为了它查看:
/WEB-INF/classes of your web application
/WEB-INF/lib/*. jar of your web application
$CATALINA_HOME/common/classes
$CATALINA_HOME/common/endorsed/*.jar
$CATALINA_HOME/common/i18n/*.jar
$CATALINA_HOME/common/lib / *. jar
$CATALINA_BASE/shared/classes
$CATALINA_BASE/shared/lib/*.jar
For example, if you put my-config.properties both in a .jar file and in WEB-INF/classes the one in WEB-INF/classes will be used. You could use this mechanism to default to test config and override prod config on prod servers.
例如,如果您将 my-config.properties 放在 .jar 文件和 WEB-INF/classes 中,则将使用 WEB-INF/classes 中的一个。您可以使用此机制默认测试配置并覆盖 prod 服务器上的 prod 配置。
回答by Jason S
Hmm. It looks like the easiest path to getting what I want on the Java side of the application is to use Servlet.getServletConfig().getInitParameter(parameterName)e.g. getInitParameter("myApp.connectionString");
唔。看起来在应用程序的 Java 端获得我想要的东西的最简单方法是使用Servlet.getServletConfig().getInitParameter(parameterName)例如 getInitParameter("myApp.connectionString");
But I don't know where to set this. The Tomcat docstalk about various permutations of context.xml but I want to make sure this parameter only affects my servlet and not any others. I also don't want to locate it within my .war file so that I can keep this parameter independent of the applications (for instance if I install an upgrade).
但我不知道在哪里设置这个。在Tomcat的文档谈论context.xml中的各种排列,但我要确保这个参数只会影响我的servlet,而不是任何其他人。我也不想在我的 .war 文件中找到它,这样我就可以保持这个参数独立于应用程序(例如,如果我安装升级)。
Update:I figured it out, key/value parameters accessible by ServletContext.getInitParameter()go here (or can go here) in ${CATALINA_HOME}/conf/server.xml:
更新:我想通了,ServletContext.getInitParameter()可访问的键/值参数在 ${CATALINA_HOME}/conf/server.xml 中转到此处(或可以转到此处):
<Server port=... >
...
<Service name="Catalina" ...>
<Engine name="Catalina" ...>
...
<Host name="localhost" ...>
<Context path="/myWarFile">
<Parameter name="foo" value="123" />
<Parameter name="bar" value="456" />
...
</Context>
</Host>
</Engine>
</Service>
</Server>
This sets two parameters, "foo" = "123", "bar" = "456" for the servlet myWarFile.war (or more accurately with the URL path /myWarFile) and I can get at them in Java with Servlet.getServletConfig().getInitParameter("foo")or Servlet.getServletConfig().getInitParameter("bar").
这为 servlet myWarFile.war(或更准确地说是 URL 路径/myWarFile)设置了两个参数,“foo”=“123”,“bar”=“456” ,我可以在 Java 中使用Servlet.getServletConfig().getInitParameter("foo")或获取它们Servlet.getServletConfig().getInitParameter("bar")。
I also looked at JIRA's server.xml entry (and what they tell you to set it to for MySQL), they use a Resourcerather than a Parameter, not quite sure of the subtleties of this but it seems like it could be more appropriate method.
我还查看了 JIRA 的 server.xml 条目(以及他们告诉您为 MySQL 设置的内容),他们使用 aResource而不是 a Parameter,不太确定这的微妙之处,但似乎它可能是更合适的方法。
<Server port=... >
<Service name="Catalina" ...>
<Engine name="Catalina" ...>
<Host name="localhost" ...>
<Context path="/jira" docBase="${catalina.home}/atlassian-jira"
reloadable="false">
<Resource name="jdbc/JiraDS" auth="Container" type="javax.sql.DataSource"
username="jirauser"
password="..."
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/jiradb1?autoReconnect=true&useUnicode=true&characterEncoding=UTF8"
maxActive="20"
validationQuery="select 1"
/>
</Context>
</Host>
</Engine>
</Service>
</Server>
回答by LE GALL Beno?t
you can add the path to your properties files in your CATALINA_HOME/conf/catalina.propertiesin the "common" classloader common.loader.
您可以CATALINA_HOME/conf/catalina.properties在“通用”类加载器common.loader 中添加属性文件的路径。

