SystemResourceLocator.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.aijia.kmt.utils;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.ApplicationContextAware;
  5. import org.springframework.stereotype.Component;
  6. import javax.annotation.PostConstruct;
  7. /**
  8. *
  9. * 类名称:SystemResourceLocator<br>
  10. * 类描述:系统属性资源存储类.<br>
  11. * 便于在其他地方调用,但添加的时候不能有重名.除非显示调用需要覆盖的接口<br>
  12. *
  13. * @version v1.0
  14. *
  15. */
  16. @Component
  17. public class SystemResourceLocator implements ApplicationContextAware {
  18. /**
  19. * spring的容器上下文
  20. */
  21. private static ApplicationContext context;
  22. /**
  23. * 实例化,执行非静态变量块
  24. */
  25. private static SystemResourceLocator instance = new SystemResourceLocator();
  26. public static RedisUtil redisUtil = null;
  27. private static String versionKey = "VERSION_*";
  28. public static final SystemResourceLocator getInstance() {
  29. return instance;
  30. }
  31. @PostConstruct
  32. public void init() {
  33. // 启动各子项目时:全量添加到redis,为保证数据的统一,覆盖旧数据,其它系统更新配置的时候一定要先同步更新到数据库
  34. redisUtil = (RedisUtil) SystemResourceLocator.getBean(RedisUtil.class);
  35. }
  36. // /**
  37. // * 取指定科目的版本列表
  38. // * @param courseName
  39. // * @return
  40. // */
  41. // public synchronized static Map<String,List<VersionConfig>> getVersionList(String courseName) {
  42. //
  43. // Map<String,List<VersionConfig>> maps=new LinkedHashMap<String, List<VersionConfig>>();
  44. // TreeSet<String> keys=redisUtil.keys(versionKey);
  45. // for (String key : keys) {
  46. // try {
  47. // if (redisUtil.exists(key.toString())){
  48. // String result = redisUtil.get(key.toString());
  49. // if(StringUtil.isNotEmpty(result)){
  50. // VersionConfig versionConfig=JSONObject.parseObject(result, VersionConfig.class);
  51. // if(StringUtil.isNotEmpty(courseName)){
  52. // if(!courseName.equals(versionConfig.getCourseName())){
  53. // continue;
  54. // }
  55. // }
  56. // if(maps.containsKey(versionConfig.getCourseName())){
  57. // maps.get(versionConfig.getCourseName()).add(versionConfig);
  58. // }else{
  59. // List<VersionConfig> list=new ArrayList<VersionConfig>();
  60. // list.add(versionConfig);
  61. // maps.put(versionConfig.getCourseName(), list);
  62. // }
  63. // }
  64. // }
  65. // } catch (Exception e) {
  66. // e.printStackTrace();
  67. // }
  68. // }
  69. // return maps;
  70. // }
  71. //
  72. // /**
  73. // * 取全部科目的版本列表
  74. // * @param
  75. // * @return
  76. // */
  77. // public synchronized static Map<String,List<VersionConfig>> getVersionList() {
  78. // return getVersionList(null);
  79. // }
  80. /**
  81. * 取值
  82. *
  83. * @param key
  84. * @return
  85. */
  86. public synchronized static Object getValue(Object key) {
  87. Object result = null;
  88. if (redisUtil.exists(key.toString())) {
  89. result = redisUtil.get(key.toString());
  90. }
  91. return result;
  92. }
  93. /**
  94. * 添加系统属性
  95. *
  96. * @param key
  97. * @param value
  98. */
  99. public synchronized static void setValue(Object key, Object value) {
  100. setValue(key, value, false);
  101. }
  102. /**
  103. * 添加系统属性
  104. *
  105. * @param key
  106. * @param value
  107. * @param isCover 是否可以覆盖
  108. */
  109. public synchronized static void setValue(Object key, Object value, boolean isCover) {
  110. if (!isCover && redisUtil.exists(key.toString())) {
  111. throw new RuntimeException("系统已存在属性" + key + ",不能覆盖该项值.");
  112. } else {
  113. redisUtil.set(key.toString(), value.toString());
  114. }
  115. }
  116. @Override
  117. public void setApplicationContext(ApplicationContext contex) throws BeansException {
  118. SystemResourceLocator.context = contex;
  119. }
  120. /**
  121. * 通过bean名称获取bean
  122. *
  123. * @param beanName
  124. * @return
  125. */
  126. public synchronized static Object getBean(String beanName) {
  127. return context.getBean(beanName);
  128. }
  129. /**
  130. * 通过class名称获取bean
  131. *
  132. * @param
  133. * @return
  134. */
  135. public static <T> T getBean(Class<T> requiredType) {
  136. return getBean(requiredType, "1.0");
  137. }
  138. public static <T> T getBean(Class<T> requiredType, String version) {
  139. return context.getBean(requiredType);
  140. }
  141. /**
  142. * 通过bean名称及类型获取bean
  143. *
  144. * @param beanName
  145. * @param requiredType
  146. * @return
  147. */
  148. public synchronized static Object getBean(String beanName, Class<?> requiredType) {
  149. return context.getBean(beanName, requiredType);
  150. }
  151. }