如何加载属性文件中的所有键/值对 [英] How to load all key/value pairs in properties file

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

问题描述

我正在尝试加载我的属性文件中的所有 key/value 对.

I'm trying to load all key/value pairs in my properties file.

一种方法是我手动使用 @Value 加载所有属性,但为此我应该知道所有的键.

One approach is that I load all the properties using @Value manually, but for that I should know all the keys.

我不能这样做,因为将来可能会更改属性文件以包含更多的 key/value 对,我可能需要再次修改 code 以适应它们.

I cannot do this, since property file may be changed in future to include more set of key/value pairs and I may need to modify code again to accommodate them.

第二种方法是我应该如何加载属性文件并迭代它以在不知道键的情况下加载所有 key/value 对.

Second approach is that I should some how load the properties file and Iterate over it to load all the key/value pairs without knowing the keys.

假设我有以下属性文件 sample.properties

Say I have following properties file sample.properties

property_set.name="Database MySQL"
db.name=
db.url=
db.user=
db.passwd=

property_set.name="Database Oracle"
db.name=
db.url=
db.user=
db.passwd=

这就是我想要做的

@Configuration
@PropertySource(value="classpath:sample.properties")
public class AppConfig {
    @Autowired
    Environment env;

    @Bean
    public void loadConfig(){
    //Can I some how iterate over the loaded sampe.properties and load all
    //key/value pair in Map<String,Map<String, String>>
    // say Map<"Database MySQL", Map<key,vale>>
    // I cannot get individual properties like env.getProperty("key"); 
    // since I may not know all the keys
}
}

推荐答案

Spring 将所有属性存储在 Environment 中.Environment 包含 PropertySource 的集合.每个 PropertySource 都包含来自特定来源的属性.有系统属性,Java 环境属性等等.您文件中的属性也会在那里.

Spring stores all properties in Environment. Environment contains collection of PropertySource. Every PropertySource contains properties from specific source. There are system properties, and java environment properties and many other. Properties from you file will be there as well.

任何来源都有自己的名字.在您的情况下,自动生成的名称将类似于 "class path resource [sample.properties]".如您所见,这个名字并不那么方便.所以让我们设置更方便的名称:

Any source has own name. In your case automatically generated name will be look like "class path resource [sample.properties]". As you see, the name is not so convenient. So lets set more convenient name:

@PropertySource(value="classpath:sample.properties", name="sample.props")

现在您可以通过此名称获取源:

Now you can get source by this name:

AbstractEnvironment ae = (AbstractEnvironment)env;
org.springframework.core.env.PropertySource source =
                              ae.getPropertySources().get("sample.props");
Properties props = (Properties)source.getSource();

注意,我指定了PropertySource 类的全名,以避免与@PropertySource 注解类冲突.之后,您可以使用属性.例如将它们输出到控制台:

Note that I specified full name of PropertySource class, to avoid conflict with @PropertySource annotation class. After that, you can work with properties. For example output them to console:

for(Object key : props.keySet()){
   System.out.println(props.get(key));
}

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

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