Global.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.ruoyi.common.config;
  2. import java.io.FileNotFoundException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import com.ruoyi.common.utils.StringUtils;
  8. import com.ruoyi.common.utils.YamlUtil;
  9. /**
  10. * 全局配置类
  11. *
  12. * @author ruoyi
  13. */
  14. public class Global
  15. {
  16. private static final Logger log = LoggerFactory.getLogger(Global.class);
  17. private static String NAME = "application.yml";
  18. /**
  19. * 当前对象实例
  20. */
  21. private static Global global = null;
  22. /**
  23. * 保存全局属性值
  24. */
  25. private static Map<String, String> map = new HashMap<String, String>();
  26. private Global()
  27. {
  28. }
  29. /**
  30. * 静态工厂方法 获取当前对象实例 多线程安全单例模式(使用双重同步锁)
  31. */
  32. public static synchronized Global getInstance()
  33. {
  34. if (global == null)
  35. {
  36. synchronized (Global.class)
  37. {
  38. if (global == null)
  39. global = new Global();
  40. }
  41. }
  42. return global;
  43. }
  44. /**
  45. * 获取配置
  46. */
  47. public static String getConfig(String key)
  48. {
  49. String value = map.get(key);
  50. if (value == null)
  51. {
  52. Map<?, ?> yamlMap = null;
  53. try
  54. {
  55. yamlMap = YamlUtil.loadYaml(NAME);
  56. value = String.valueOf(YamlUtil.getProperty(yamlMap, key));
  57. map.put(key, value != null ? value : StringUtils.EMPTY);
  58. }
  59. catch (FileNotFoundException e)
  60. {
  61. log.error("获取全局配置异常 {}", key);
  62. }
  63. }
  64. return value;
  65. }
  66. /**
  67. * 获取项目名称
  68. */
  69. public static String getName()
  70. {
  71. return StringUtils.nvl(getConfig("ruoyi.name"), "RuoYi");
  72. }
  73. /**
  74. * 获取项目版本
  75. */
  76. public static String getVersion()
  77. {
  78. return StringUtils.nvl(getConfig("ruoyi.version"), "3.1.0");
  79. }
  80. /**
  81. * 获取版权年份
  82. */
  83. public static String getCopyrightYear()
  84. {
  85. return StringUtils.nvl(getConfig("ruoyi.copyrightYear"), "2018");
  86. }
  87. /**
  88. * 获取ip地址开关
  89. */
  90. public static Boolean isAddressEnabled()
  91. {
  92. return Boolean.valueOf(getConfig("ruoyi.addressEnabled"));
  93. }
  94. /**
  95. * 获取文件上传路径
  96. */
  97. public static String getProfile()
  98. {
  99. return getConfig("ruoyi.profile");
  100. }
  101. /**
  102. * 获取头像上传路径
  103. */
  104. public static String getAvatarPath()
  105. {
  106. return getConfig("ruoyi.profile") + "avatar/";
  107. }
  108. /**
  109. * 获取下载上传路径
  110. */
  111. public static String getDownloadPath()
  112. {
  113. return getConfig("ruoyi.profile") + "download/";
  114. }
  115. /**
  116. * 获取作者
  117. */
  118. public static String getAuthor()
  119. {
  120. return StringUtils.nvl(getConfig("gen.author"), "ruoyi");
  121. }
  122. /**
  123. * 生成包路径
  124. */
  125. public static String getPackageName()
  126. {
  127. return StringUtils.nvl(getConfig("gen.packageName"), "com.ruoyi.project.module");
  128. }
  129. /**
  130. * 是否自动去除表前缀
  131. */
  132. public static String getAutoRemovePre()
  133. {
  134. return StringUtils.nvl(getConfig("gen.autoRemovePre"), "true");
  135. }
  136. /**
  137. * 表前缀(类名不会包含表前缀)
  138. */
  139. public static String getTablePrefix()
  140. {
  141. return StringUtils.nvl(getConfig("gen.tablePrefix"), "sys_");
  142. }
  143. }