多个属性文件 [英] Multiple properties files

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

问题描述

在java应用程序中,我使用.properties文件来访问与应用程序相关的配置属性。

例如。

AppConfig.properties 其中的内容是,

In a java application, I am using .properties file to access application related config properties.
For eg.
AppConfig.properties the contents of which are say,

settings.user1.name=userone
settings.user2.name=usertwo
settings.user1.password=Passwrd1!
settings.user2.password=Passwrd2!  

我通过java文件访问这些属性 - AppConfiguration.java 喜欢

I am accesing these properties through a java file - AppConfiguration.java like

    private final Properties properties = new Properties();
    public AppConfiguration(){
        properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig.properties"));
}

现在,我没有将所有键值属性保存在一个文件中,而是想把它们分成几个文件(AppConfig1.properties,AppConfig2.properties,AppConfig3.properties等)。

我想知道是否可以同时加载这些多个文件。

Now, instead of keeping all the key-value properties in one file, I would like to divide them in few files(AppConfig1.properties, AppConfig2.properties, AppConfig3.properties etc.).
I would like to know if it is possible to load these multiple files simultaneously.

我的问题与 - Java项目中的多个.properties文件

谢谢。

推荐答案

是的。只需要多个加载语句。

Yes. Simply have multiple load statements.

properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig1.properties"));
properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig2.properties"));
properties.load(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("AppConfig2.properties"));

所有键值对都可以使用属性对象使用。

All the key-value pairs will be available to use using the properties object.

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

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