Java项目中的多个.properties文件 [英] Multiple .properties files in a Java project

查看:1245
本文介绍了Java项目中的多个.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种方便的方法,在不同部署配置的多个 .properties 文件之间切换。



我的初始倾向是创建一个单独的文件 selector.properties ,其单个属性用于确定正确的文件:



properties.file = deploymentConfiguration1.properties



p>

properties.file = deploymentConfiguration2.properties





我团队的另一位开发人员拥有 ApplicationProperties 课程,其中:



private static final String PROP_FILE =someFileName.properties;



...是做这个的手段。但是,我要切换属性文件而不重建!预先感谢您的输入。



编辑:也许我最初应该更清楚,但这是一套web服务打包作为AAR。我将把它放到Web服务器中,让Tomcat和Axis2做他们的事情。



所以,我不认为一些命令行参数的答案

解决方案

拥有多个属性文件,每个env一个,例如

  application-dev.properties 
application-test.properties
application-prod.properties

使用env作为系统属性启动应用程序;

  java -Denv = test 

从相关文件加载属性;

  String props =application-+ System.getProperty(env)+.properties; 

请注意,我通常不建议使用固定名称的属性文件

编辑 如果是网络应用程序,您可以在部署描述符(web.xml)中设置env的值

 < env-entry> 
< env-entry-name> myEnv< / env-entry-name>
< env-entry-type> java.lang.String< / env-entry-type>
< env-entry-value> test< / env-entry-value>
< / env-entry>

那么你可以像这样得到你的网络应用程序中的值;

 上下文ctx = new InitialContext(); 
上下文envCtx =(Context)ctx.lookup(java:comp / env);
String env =(String)envCtx.lookup(myEnv);
String props =application-+ env +.properties;


I would like to find an expedient way to switch between multiple .properties files for different deployment configurations.

My initial inclination is to create a separate file, selector.properties, whose single property is used to determine the proper file:

properties.file=deploymentConfiguration1.properties

...for one deployment, or:

properties.file=deploymentConfiguration2.properties

...for the next deployment.

Another developer on my team has an ApplicationProperties class wherein:

private static final String PROP_FILE="someFileName.properties";

...is the means to do this. However, I want to switch properties files without rebuilding! Thanks in advance for your input.

EDIT: Maybe I should have been more clear initially, but this is for a set of web services packaged as an AAR. I will just drop it into the web server and let Tomcat and Axis2 do their thing.

So, I don't think some of the answers with command-line params will work in this context.

解决方案

Have multiple property files, one per env, eg;

application-dev.properties
application-test.properties
application-prod.properties

Launch your app with the env as a system property;

java -Denv=test

Load your properties from the relevant file;

String props = "application-" + System.getProperty("env") + ".properties";

Note that I'd generally discourage this in favour of a properties file with a fixed name where the file is generated at build time.

Edit:
If it's a web app, you can set the value of env in the deployment descriptor (web.xml)

<env-entry>
  <env-entry-name>myEnv</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>test</env-entry-value>
</env-entry>

then you can get the value in your web app like this;

Context ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
String env = (String)envCtx.lookup("myEnv");
String props = "application-" + env + ".properties";

这篇关于Java项目中的多个.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆