Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

Spring通用PropertiesUtil

代码实现

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
public class PropertiesUtil extends PropertyPlaceholderConfigurer implements
Map<String, String> {

private static final Logger logger = Logger.getLogger(PropertiesUtil.class);
private static Map<String, String> ctxPropertiesMap;

protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
if (ctxPropertiesMap != null) {
logger.warn("The property map will be override!");
}
ctxPropertiesMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}

public static String getString(String name) {
if (ctxPropertiesMap == null) {
ctxPropertiesMap = new HashMap<String, String>();
}
return (String) ctxPropertiesMap.get(name);
}

public static boolean getBoolean(String name, boolean defaultValue) {
String v = getString(name);
if (v == null) {
return defaultValue;
}
try {
return Boolean.parseBoolean(v);
} catch (Exception e) {
}
return defaultValue;
}

public static int getIntValue(String name, int defaultValue) {
String v = getString(name);
if (v == null) {
return defaultValue;
}
try {
return Integer.parseInt(v);
} catch (Exception e) {
}
return defaultValue;
}

public static long getLongValue(String name, long defaultValue) {
String v = getString(name);
if (v == null) {
return defaultValue;
}
try {
return Long.parseLong(v);
} catch (Exception e) {
}
return defaultValue;
}

public static short getShortValue(String name, short defaultValue) {
String v = getString(name);
if (v == null) {
return defaultValue;
}
try {
return Short.parseShort(v);
} catch (Exception e) {
}
return defaultValue;
}

public static double getDoubleValue(String name, double defaultValue) {
String v = getString(name);
if (v == null) {
return defaultValue;
}
try {
return Double.parseDouble(v);
} catch (Exception e) {
}
return defaultValue;
}

public static float getFloatValue(String name, float defaultValue) {
String v = getString(name);
if (v == null) {
return defaultValue;
}
try {
return Float.parseFloat(v);
} catch (Exception e) {
}
return defaultValue;
}

public int size() {
return ctxPropertiesMap.size();
}

public boolean isEmpty() {
return ctxPropertiesMap.isEmpty();
}

public boolean containsKey(Object key) {
return ctxPropertiesMap.containsKey(key);
}

public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}

public String put(String key, String value) {
throw new UnsupportedOperationException();
}

public String remove(Object key) {
throw new UnsupportedOperationException();
}

public void putAll(Map<? extends String, ? extends String> m) {
throw new UnsupportedOperationException();
}

public void clear() {
throw new UnsupportedOperationException();
}

public Set<String> keySet() {
throw new UnsupportedOperationException();
}

public Collection<String> values() {
throw new UnsupportedOperationException();
}

public Set<Map.Entry<String, String>> entrySet() {
throw new UnsupportedOperationException();
}

public String get(Object key) {
return (String) ctxPropertiesMap.get(key);
}

使用方式,创建一个上面类的bean:

1
2
3
4
5
6
7
8
<bean id="propertyConfigurer"
class="com.forg.common.PropertiesUtil">
<property name="locations">
<list>
<value>classpath:system.properties</value>
</list>
</property>
</bean>

这样就可以使用了~