Spring读取配置文件

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.tre.common.utils;

import java.io.UnsupportedEncodingException;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.springframework.stereotype.Component;

@Component
public class readUtil {

//ccl 目录 /resources/ccl.properties
public Map<String, Object> readProperty() {
ResourceBundle resourceBundle = ResourceBundle.getBundle("ccl");
Map<String, Object> property = new HashMap<>();
//遍历取值
Enumeration enumeration = resourceBundle.getKeys();
while (enumeration.hasMoreElements()) {
try {
String key = (String) enumeration.nextElement();
String value = resourceBundle.getString(key);
String result = new String(value.getBytes("iso-8859-1"), "gbk");
property.put(key, value);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

return property;

}
}