简体   繁体   English

在Java(Servlet)中放置应用程序范围的设置?

[英]Where to put application-wide settings in Java (Servlet)?

In ASP.NET, there is web.config which can hold application-wide settings. 在ASP.NET中,有web.config可以保存应用程序范围的设置。 Is there a corresponding file (residing outside of the war or jar archive) for a Java EE Servlet? Java EE Servlet是否有相应的文件(驻留在warjar存档之外)?

What I need is some place to point out a configuration file, which currently holds four attributes which in turn, taken together, leads to the database where the rest of the data and configuration is stored. 我需要的是指出一个配置文件的地方,该配置文件当前包含四个属性,这些属性反过来一起导致存储其余数据和配置的数据库。 (Server, database, username and password.) These values need to be easy to change without repackaging and redeploying the entire application, hence the configuration file, but hardcoding the path to the configuration file in the application (even if it is as a constant) seems far from optimal. (服务器,数据库,用户名和密码。)这些值需要易于更改,无需重新打包和重新部署整个应用程序,因此配置文件,但硬编码应用程序中配置文件的路径(即使它是一个常量)似乎远非最佳。

Any hints? 任何提示? I've tried Google but found very little that seemed relevant - and what I did find appeared hideously over-engineered for my needs. 我已经尝试了谷歌,但发现它似乎很少相关 - 而我所发现的东西看起来似乎过于设计,以满足我的需求。

In ASP.NET, there is web.config which can hold application-wide settings. 在ASP.NET中,有web.config可以保存应用程序范围的设置。 Is there a corresponding file (residing outside of the war or jar archive) for a Java EE Servlet? Java EE Servlet是否有相应的文件(驻留在war或jar存档之外)?

That's the web.xml . 这是web.xml You can define settings as <context-param> entries. 您可以将设置定义为<context-param>条目。

<context-param>
    <param-name>foo</param-name>
    <param-value>bar</param-value>
</context-param>

It's available by ServletContext#getInitParameter() . 它可以通过ServletContext#getInitParameter() The ServletContext is in turn available anywhere. ServletContext随后可以在任何地方使用。

String foo = getServletContext().getInitParameter("foo"); // Contains "bar"

You can also access it by EL. 您也可以通过EL访问它。

#{initParam.foo} <!-- prints "bar" -->

What I need is some place to point out a configuration file, which currently holds four attributes which in turn, taken together, leads to the database where the rest of the data and configuration is stored. 我需要的是指出一个配置文件的地方,该配置文件当前包含四个属性,这些属性反过来一起导致存储其余数据和配置的数据库。 (Server, database, username and password.) These values need to be easy to change without repackaging and redeploying the entire application , hence the configuration file, but hardcoding the path to the configuration file in the application (even if it is as a constant) seems far from optimal. (服务器,数据库,用户名和密码。) 这些值需要易于更改,无需重新打包和重新部署整个应用程序 ,因此配置文件,但硬编码应用程序中配置文件的路径(即使它是一个常量)似乎远非最佳。

As per the emphasis, I'd use a properties file for this particular purpose which is then placed in a path outside the WAR. 根据重点,我将使用属性文件用于此特定目的,然后将其放置在WAR外部的路径中。 You just need to add this path to the Java runtime classpath. 您只需将此路径添加到Java运行时类路径即可。 Then you can obtain it as classpath resource: 然后您可以将其作为类路径资源获取:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
// ...

However, with the particular sole purpose to serve a DB connection, you're indeed better off with a servletcontainer-managed datasource as answered by Qwerky. 但是,由于服务于数据库连接的唯一目的,您最好使用由Qwerky回答的servletcontainer管理的数据源。 All you possibly would need to configure is then just the datasource name. 您可能需要配置的只是数据源名称。

If this is a web app then you'd be better served configuring the database connection as a resource on the server, then getting your app to retrieve it using JNDI. 如果这是一个Web应用程序,那么您最好将数据库连接配置为服务器上的资源,然后让您的应用程序使用JNDI检索它。 Your app server will have documentation on how to do this, its a basic task. 您的应用服务器将提供有关如何执行此操作的文档,这是一项基本任务。

99% of serious web apps do this, the other 1% should. 99%的严肃的网络应用程序执行此操作,另外1%应该这样做。

You can have your application load an arbitrary external file by simply passing the path as a command-line parameter (to the servlet container startup script). 只需将路径作为命令行参数传递给servlet容器启动脚本,就可以让应用程序加载任意外部文件。 Then store the values in the ServletContext 然后将值存储在ServletContext

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

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