SystemConfig.java
3.37 KB
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
/**
* Project Name:HSCM File Name:SystemConfig.java Package Name:com.phxl.hscm.common.util Date:2015年6月29日下午5:13:50 Copyright (c) 2015, PHXL All Rights Reserved.
*/
package com.phxl.common.util;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
/**
* 系统配置工具<br/>
* 加载系统Properties配置文件至内存中<br/>
* @Date 2015年6月29日 下午5:13:50
* @author shijingbang
* @version 1.0
* @since JDK 1.6
*/
@Component
public class SystemConfig {
private static final Logger logger = LoggerFactory.getLogger(SystemConfig.class);
private static final Properties properties = new Properties();
@PostConstruct
public void init() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(this.getClass().getClassLoader());
//Resource[] resources = resolver.getResources("classpath:**/spring/*.properties");
Resource[] resources = resolver.getResources("classpath:**/*.properties");
for (Resource resource : resources) {
if (logger.isDebugEnabled()) {
logger.debug("加载Properties配置文件{}", resource.getFilename());
}
properties.load(resource.getInputStream());
}
if(logger.isDebugEnabled()){
Map<String, Object> configuration = MapUtils.typedSortedMap(new TreeMap(properties), String.class, Object.class);
//logger.debug("****************系统配置参数表****************\n{}", JSONUtils.toPrettyJsonLoosely(configuration));
}
}
/**
* 根据key找出value值
* @param key 键
* @return String
* @author shijingbang
*/
public static String getProperty(String key) {
return properties.getProperty(key);
}
/**
* 根据key找出value值.没有找到则返回第二个参数的值
* @param key 键
* @param defaultValue 默认值
* @return String
* @author shijingbang
*/
public static String getProperty(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
/**
* 获取整数参数值
* @param key
* @return Integer
*/
public static Integer getIntProperty(String key){
String value = properties.getProperty(key);
if(StringUtils.isNotBlank(value)){
return Integer.parseInt(value);
}
return null;
}
/**
* 获取整数参数值,必需指定默认值
* @param key
* @param defaultValue
* @return int
*/
public static int getIntProperty(String key, Integer defaultValue){
//LocalAssert.notNull(defaultValue, "defaultValue,不能为空");
String value = properties.getProperty(key);
if(StringUtils.isBlank(value)){
return defaultValue;
}
return Integer.parseInt(value);
}
/**
* 检查是否包含指定key
* @param key
* @return boolean
* @author shijingbang
*/
public static boolean containsKey(Object key) {
return properties.containsKey(key);
}
/**
* 检查系统环境是否包含指定值
* @param value
* @return boolean
* @author shijingbang
*/
public static boolean containsValue(Object value) {
return properties.containsValue(value);
}
}