java加载多个properties文件,Java项目中的多个.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)

myEnv

java.lang.String

test

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";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值