System.getProperty(" catalina.base")可能存在客户端可能使用任何其他服务器的情况 [英] System.getProperty("catalina.base") There can be scenario where client may use any other server
问题描述
我正在使用
<$ p从
Tomcat \ conf\somename.properties
目录中读取属性文件$ p> String demo = System.getProperty(catalina.base)+
File.separator +conf+ File.separator +somename.properties;
这对Tomcat完全正常。但是,可能存在客户端可能使用任何其他服务器(如Glassfish或Websphere)的情况,在这种情况下,我将无法获得 System.getProperty(catalina.base)
。
我该如何妥善解决?我能够使用 ResourceBundle
来做到这一点,但为此我必须将我的属性文件保存在我的构建中,这是我不想要的。我只是想从我的构建之外读取我的属性文件。
基本上有两种方法。
-
只需将其路径添加到运行时类路径中,以便您可以通常的方式从类路径中获取它。对于Tomcat,您可以通过在
/conf/catalina.properties <的
shared.loader
属性中指定外部文件夹,将其添加到运行时类路径中。 / code>。例如shared.loader = $ {catalina.home} / conf
或更好,不要特定于服务器
shared.loader = / path / to / folder
其他服务器还支持将外部文件夹添加到类路径中,请参阅其文档。
这样你就可以从类路径中得到一个
InputStream
,如下所示:InputStream input = Thread.currentThread()。getContextClassLoader()。getResourceAsStream(/ config.properties);
属性properties = new Properties();
properties.load(输入);
// ...
-
自己添加另一个独立于服务器的系统属性,你可以设置为VM参数。
-Dconfig.location = / path / to / folder
如果是Tomcat,您可以将其设置为
JAVA_OPTS
环境变量,或者编辑catalina.bat
启动文件或编辑Windows服务设置(当它作为Windows服务安装时)等。其他服务器也支持类似的结构。
这样你可以按如下方式获得它
文件文件=新文件(System.getProperty(config.location ),config.properties);
InputStream input = new FileInputStream(file);
属性properties = new Properties();
properties.load(输入);
// ...
无论您选择哪种方式,在分发应用程序时,都应正确记录,以便serveradmin可以相应地对其进行配置。
无关问题, ResourceBundle
不是读取配置属性文件的正确方法。它旨在用于国际化的本地化内容。
I am reading a properties file from the Tomcat\conf\somename.properties
directory using
String demo = System.getProperty("catalina.base") +
File.separator + "conf" + File.separator + "somename.properties";
This is working perfectly fine with Tomcat. But, there can be scenario where client may use any other server like Glassfish or Websphere, in that case I won't be able to get System.getProperty("catalina.base")
.
How should I solve it properly? I'm able to do that using ResourceBundle
but for that I have to keep my properties file in my build, which I don't want. I just want to read my properties file from outside my build.
There are basically two ways.
Just add its path to the runtime classpath so that you can get it from the classpath the usual way. In case of Tomcat, you can add external folders to the runtime classpath by specifying it in the
shared.loader
property of/conf/catalina.properties
. E.g.shared.loader = ${catalina.home}/conf
Or better, don't be server-specific
shared.loader = /path/to/folder
Other servers also supports adding external folders to the classpath, consult their documentation.
This way you'll be able to get an
InputStream
of it from the classpath as follows:InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("/config.properties"); Properties properties = new Properties(); properties.load(input); // ...
Add another server-independent system property yourself, you can set as a VM argument.
-Dconfig.location=/path/to/folder
In case of Tomcat, you can set it as
JAVA_OPTS
environment variable, or edit thecatalina.bat
startup file or edit the Windows Service settings (when it's installed as Windows Service), etc. Other servers supports similar constructs as well.This way you can obtain it as follows
File file = new File(System.getProperty("config.location"), "config.properties"); InputStream input = new FileInputStream(file); Properties properties = new Properties(); properties.load(input); // ...
Either way you choose, when distributing your application, you should document it properly so that the serveradmin can configure it accordingly.
Unrelated to the problem, the ResourceBundle
is not the right way to read configuration properties files. It's intented for localized content for internationalization.
这篇关于System.getProperty(" catalina.base")可能存在客户端可能使用任何其他服务器的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!