PropertiesLoader.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright (c) 2005-2011 springside.org.cn
  3. *
  4. * $Id: PropertiesLoader.java 1690 2012-02-22 13:42:00Z calvinxiu $
  5. */
  6. package com.huaxu.common;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.NoSuchElementException;
  10. import java.util.Properties;
  11. import org.apache.commons.io.IOUtils;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.core.io.DefaultResourceLoader;
  15. import org.springframework.core.io.Resource;
  16. import org.springframework.core.io.ResourceLoader;
  17. /**
  18. * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
  19. * @author calvin
  20. * @version 2013-05-15
  21. */
  22. public class PropertiesLoader {
  23. private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
  24. private static ResourceLoader resourceLoader = new DefaultResourceLoader();
  25. private final Properties properties;
  26. public PropertiesLoader(String... resourcesPaths) {
  27. properties = loadProperties(resourcesPaths);
  28. }
  29. public Properties getProperties() {
  30. return properties;
  31. }
  32. /**
  33. * 取出Property,但以System的Property优先,取不到返回空字符串.
  34. */
  35. private String getValue(String key) {
  36. String systemProperty = System.getProperty(key);
  37. if (systemProperty != null) {
  38. return systemProperty;
  39. }
  40. if (properties.containsKey(key)) {
  41. return properties.getProperty(key);
  42. }
  43. return "";
  44. }
  45. /**
  46. * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
  47. */
  48. public String getProperty(String key) {
  49. String value = getValue(key);
  50. if (value == null) {
  51. throw new NoSuchElementException();
  52. }
  53. return value;
  54. }
  55. /**
  56. * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
  57. */
  58. public String getProperty(String key, String defaultValue) {
  59. String value = getValue(key);
  60. return value != null ? value : defaultValue;
  61. }
  62. /**
  63. * 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
  64. */
  65. public Integer getInteger(String key) {
  66. String value = getValue(key);
  67. if (value == null) {
  68. throw new NoSuchElementException();
  69. }
  70. return Integer.valueOf(value);
  71. }
  72. /**
  73. * 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
  74. */
  75. public Integer getInteger(String key, Integer defaultValue) {
  76. String value = getValue(key);
  77. return value != null ? Integer.valueOf(value) : defaultValue;
  78. }
  79. /**
  80. * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
  81. */
  82. public Double getDouble(String key) {
  83. String value = getValue(key);
  84. if (value == null) {
  85. throw new NoSuchElementException();
  86. }
  87. return Double.valueOf(value);
  88. }
  89. /**
  90. * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
  91. */
  92. public Double getDouble(String key, Integer defaultValue) {
  93. String value = getValue(key);
  94. return value != null ? Double.valueOf(value) : defaultValue;
  95. }
  96. /**
  97. * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
  98. */
  99. public Boolean getBoolean(String key) {
  100. String value = getValue(key);
  101. if (value == null) {
  102. throw new NoSuchElementException();
  103. }
  104. return Boolean.valueOf(value);
  105. }
  106. /**
  107. * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
  108. */
  109. public Boolean getBoolean(String key, boolean defaultValue) {
  110. String value = getValue(key);
  111. return value != null ? Boolean.valueOf(value) : defaultValue;
  112. }
  113. /**
  114. * 载入多个文件, 文件路径使用Spring Resource格式.
  115. */
  116. private Properties loadProperties(String... resourcesPaths) {
  117. Properties props = new Properties();
  118. for (String location : resourcesPaths) {
  119. // logger.debug("Loading properties file from:" + location);
  120. InputStream is = null;
  121. try {
  122. Resource resource = resourceLoader.getResource(location);
  123. is = resource.getInputStream();
  124. props.load(is);
  125. } catch (IOException ex) {
  126. logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
  127. } finally {
  128. IOUtils.closeQuietly(is);
  129. }
  130. }
  131. return props;
  132. }
  133. }