From cb13642e85a9d572d6f88f6ce7e3d7b0893dd713 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期五, 13 八月 2021 21:09:24 +0800 Subject: [PATCH] update 重写 OSS 模块相关实现 支持动态配置(页面配置) --- ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java | 36 ++++++++++++++++++++++++------------ 1 files changed, 24 insertions(+), 12 deletions(-) diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java index 90e21ab..ea64b27 100644 --- a/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java @@ -1,15 +1,16 @@ package com.ruoyi.oss.factory; import cn.hutool.core.convert.Convert; -import cn.hutool.core.lang.Assert; -import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.redis.RedisCache; +import com.ruoyi.common.utils.JsonUtils; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.reflect.ReflectUtils; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.oss.constant.CloudConstant; import com.ruoyi.oss.enumd.CloudServiceEnumd; import com.ruoyi.oss.exception.OssException; -import com.ruoyi.oss.service.ICloudStorageService; +import com.ruoyi.oss.properties.CloudStorageProperties; +import com.ruoyi.oss.service.ICloudStorageStrategy; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -27,26 +28,37 @@ OssFactory.redisCache = SpringUtils.getBean(RedisCache.class); } - private static final Map<String, ICloudStorageService> SERVICES = new ConcurrentHashMap<>(); + private static final Map<String, ICloudStorageStrategy> SERVICES = new ConcurrentHashMap<>(); - public static ICloudStorageService instance() { - String type = Convert.toStr(redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + CloudConstant.CLOUD_STORAGE_CONFIG_KEY)); + public static ICloudStorageStrategy instance() { + String type = Convert.toStr(redisCache.getCacheObject(CloudConstant.CACHE_CONFIG_KEY)); if (StringUtils.isEmpty(type)) { throw new OssException("鏂囦欢瀛樺偍鏈嶅姟绫诲瀷鏃犳硶鎵惧埌!"); } return instance(type); } - public static ICloudStorageService instance(String type) { - ICloudStorageService service = SERVICES.get(type); + public static ICloudStorageStrategy instance(String type) { + ICloudStorageStrategy service = SERVICES.get(type); if (service == null) { - service = (ICloudStorageService) SpringUtils.getBean(CloudServiceEnumd.getServiceClass(type)); + Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type); + CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class); + String beanName = CloudServiceEnumd.getServiceName(type); + ICloudStorageStrategy bean = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type), properties); + SpringUtils.registerBean(beanName, bean); + service = SpringUtils.getBean(beanName); + SERVICES.put(type, bean); } return service; } - public static void register(String type, ICloudStorageService iCloudStorageService) { - Assert.notNull(type, "type can't be null"); - SERVICES.put(type, iCloudStorageService); + public static void destroy(String type) { + ICloudStorageStrategy service = SERVICES.get(type); + if (service == null) { + return; + } + SpringUtils.unregisterBean(CloudServiceEnumd.getServiceName(type)); + SERVICES.remove(type); } + } -- Gitblit v1.9.3