From af0e0a110e7187bf008655f7510199a0c0b25ec4 Mon Sep 17 00:00:00 2001 From: Nymph2333 <498092988@qq.com> Date: 星期一, 10 四月 2023 14:27:40 +0800 Subject: [PATCH] newInstance() 已弃用,使用clazz.getDeclaredConstructor().newInstance() This method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException. The call clazz.newInstance() can be replaced by clazz.getDeclaredConstructor().newInstance() The latter sequence of calls is inferred to be able to throw the additional exception types InvocationTargetException and NoSuchMethodException. Both of these exception types are subclasses of ReflectiveOperationException. --- ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java | 53 +++++++++++++++++++++++++++++++++-------------------- 1 files changed, 33 insertions(+), 20 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java index e40b0c0..39ad84a 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java @@ -2,7 +2,8 @@ import java.util.Collection; import java.util.List; -import com.ruoyi.common.constant.Constants; +import com.alibaba.fastjson2.JSONArray; +import com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.core.domain.entity.SysDictData; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.spring.SpringUtils; @@ -38,11 +39,10 @@ */ public static List<SysDictData> getDictCache(String key) { - Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key)); - if (StringUtils.isNotNull(cacheObj)) + JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key)); + if (StringUtils.isNotNull(arrayCache)) { - List<SysDictData> DictDatas = StringUtils.cast(cacheObj); - return DictDatas; + return arrayCache.toList(SysDictData.class); } return null; } @@ -84,27 +84,30 @@ StringBuilder propertyString = new StringBuilder(); List<SysDictData> datas = getDictCache(dictType); - if (StringUtils.containsAny(separator, dictValue) && StringUtils.isNotEmpty(datas)) + if (StringUtils.isNotNull(datas)) { - for (SysDictData dict : datas) + if (StringUtils.containsAny(separator, dictValue)) { - for (String value : dictValue.split(separator)) + for (SysDictData dict : datas) { - if (value.equals(dict.getDictValue())) + for (String value : dictValue.split(separator)) { - propertyString.append(dict.getDictLabel() + separator); - break; + if (value.equals(dict.getDictValue())) + { + propertyString.append(dict.getDictLabel()).append(separator); + break; + } } } } - } - else - { - for (SysDictData dict : datas) + else { - if (dictValue.equals(dict.getDictValue())) + for (SysDictData dict : datas) { - return dict.getDictLabel(); + if (dictValue.equals(dict.getDictValue())) + { + return dict.getDictLabel(); + } } } } @@ -132,7 +135,7 @@ { if (label.equals(dict.getDictLabel())) { - propertyString.append(dict.getDictValue() + separator); + propertyString.append(dict.getDictValue()).append(separator); break; } } @@ -152,11 +155,21 @@ } /** + * 删除指定字典缓存 + * + * @param key 字典键 + */ + public static void removeDictCache(String key) + { + SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(key)); + } + + /** * 清空字典缓存 */ public static void clearDictCache() { - Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*"); + Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.SYS_DICT_KEY + "*"); SpringUtils.getBean(RedisCache.class).deleteObject(keys); } @@ -168,6 +181,6 @@ */ public static String getCacheKey(String configKey) { - return Constants.SYS_DICT_KEY + configKey; + return CacheConstants.SYS_DICT_KEY + configKey; } } -- Gitblit v1.9.2