Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package com.core.bean.memcache.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.NoSuchElementException;
  6. import java.util.Properties;
  7.  
  8. import org.apache.commons.io.IOUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.core.io.DefaultResourceLoader;
  12. import org.springframework.core.io.Resource;
  13. import org.springframework.core.io.ResourceLoader;
  14.  
  15. /**
  16. * Properties文件载入工具类. 可载入多个properties文件,
  17. * 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
  18. *
  19. */
  20. public class PropertiesLoader {
  21.  
  22. private static Logger logger = LoggerFactory
  23. .getLogger(PropertiesLoader.class);
  24.  
  25. private static ResourceLoader resourceLoader = new DefaultResourceLoader();
  26.  
  27. private final Properties properties;
  28.  
  29. public PropertiesLoader(String... resourcesPaths) {
  30. properties = loadProperties(resourcesPaths);
  31. }
  32.  
  33. public Properties getProperties() {
  34. return properties;
  35. }
  36.  
  37. /**
  38. * 取出Property,但以System的Property优先,取不到返回空字符串.
  39. */
  40. private String getValue(String key) {
  41. String systemProperty = System.getProperty(key);
  42. if (systemProperty != null) {
  43. return systemProperty;
  44. }
  45. if (properties.containsKey(key)) {
  46. return properties.getProperty(key);
  47. }
  48. return "";
  49. }
  50.  
  51. /**
  52. * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
  53. */
  54. public String getProperty(String key) {
  55. String value = getValue(key);
  56. if (value == null) {
  57. throw new NoSuchElementException();
  58. }
  59. return value;
  60. }
  61.  
  62. /**
  63. * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
  64. */
  65. public String getProperty(String key, String defaultValue) {
  66. String value = getValue(key);
  67. return value != null ? value : defaultValue;
  68. }
  69.  
  70. /**
  71. * 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
  72. */
  73. public Integer getInteger(String key) {
  74. String value = getValue(key);
  75. if (value == null) {
  76. throw new NoSuchElementException();
  77. }
  78. return Integer.valueOf(value);
  79. }
  80.  
  81. /**
  82. * 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
  83. */
  84. public Integer getInteger(String key, Integer defaultValue) {
  85. String value = getValue(key);
  86. return value != null ? Integer.valueOf(value) : defaultValue;
  87. }
  88.  
  89. /**
  90. * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
  91. */
  92. public Double getDouble(String key) {
  93. String value = getValue(key);
  94. if (value == null) {
  95. throw new NoSuchElementException();
  96. }
  97. return Double.valueOf(value);
  98. }
  99.  
  100. /**
  101. * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
  102. */
  103. public Double getDouble(String key, Integer defaultValue) {
  104. String value = getValue(key);
  105. return value != null ? Double.valueOf(value) : defaultValue;
  106. }
  107.  
  108. /**
  109. * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/
  110. * false则返回false.
  111. */
  112. public Boolean getBoolean(String key) {
  113. String value = getValue(key);
  114. if (value == null) {
  115. throw new NoSuchElementException();
  116. }
  117. return Boolean.valueOf(value);
  118. }
  119.  
  120. /**
  121. * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/
  122. * false则返回false.
  123. */
  124. public Boolean getBoolean(String key, boolean defaultValue) {
  125. String value = getValue(key);
  126. return value != null ? Boolean.valueOf(value) : defaultValue;
  127. }
  128.  
  129. /**
  130. * 载入多个文件, 文件路径使用Spring Resource格式.
  131. */
  132. private Properties loadProperties(String... resourcesPaths) {
  133. Properties props = new Properties();
  134. for (String location : resourcesPaths) {
  135. InputStream is = null;
  136. try {
  137. Resource resource = resourceLoader.getResource(location);
  138. is = resource.getInputStream();
  139. props.load(is);
  140. } catch (IOException ex) {
  141. logger.info("Could not load properties from path:" + location
  142. + ", " + ex.getMessage());
  143. } finally {
  144. IOUtils.closeQuietly(is);
  145. }
  146. }
  147. return props;
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement