简体   繁体   English

在部署Java Web应用程序时,如何知道.properties文件的位置以读取配置?

[英]How to know the location of a .properties file to read configurations when deploying a Java web application?

According to what I know Java Web applications are deployed on a server by deploying the .war file of the web application. 据我所知,通过部署Web应用程序的.war文件,可以将Java Web应用程序部署在服务器上。 Is this correct? 这个对吗?

I have a Web application that has some configurations to allow the server administrator to configure the application at the time of deployment. 我有一个Web应用程序,该应用程序具有一些配置,允许服务器管理员在部署时配置该应用程序。 Ex. 防爆。 Set the path where log files are to be saved, Set the location to store uploaded documents etc.. 设置保存日志文件的路径,设置存储上载文档的位置等。

I defined such parameters in a .properties file and hardcode the path to it. 我在.properties文件中定义了此类参数,并将其路径硬编码。 Thereafter I was able to get the web application to read the values in the .properties file without recompiling the source code (as the .properties file path did not change). 之后,我能够使Web应用程序读取.properties文件中的值,而无需重新编译源代码(因为.properties文件路径未更改)。
Now I want to know how to do this without hardcoding the path to the .properties file so I can deploy this on any server without worrying about its directory structure. 现在,我想知道如何在不对.properties文件的路径进行硬编码的情况下执行此操作,因此我可以将其部署在任何服务器上,而不必担心其目录结构。

So, is it possible to provide a .properties file along with the .war file when the web application is being deployed so that the server administrator can edit the .properties configuration file , deploy the .war file (without recompiling the web application) on a server and let it read the values in the .properties file? 因此,可以在部署Web应用程序时将.properties文件与.war文件一起提供,以便服务器管理员可以编辑.properties配置文件 ,部署.war文件(无需重新编译Web应用程序)。服务器,并让它读取.properties文件中的值?
If so how can the web application know where the .properties file is? 如果是这样,Web应用程序如何知道.properties文件在哪里?

Best practise is to put the properties file at a location where it can be found on the classpath. 最佳实践是将属性文件放在类路径中可以找到的位置。 Your application loads the file with 您的应用程序使用

this.getClass().getClassLoader().getResourceAsStream( "myConfig.properties");

If the file is located in a non standard classpath location you can finetune your applications classpath using tomcats configuration possibilites. 如果文件位于非标准类路径位置,则可以使用tomcats配置可能性微调应用程序的类路径。 See eg Understanding The Tomcat Classpath 请参阅例如了解Tomcat类路径

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

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