From fd83d6bc0a47f6ae37fca760416544b938a1bff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Wed, 7 Dec 2022 19:17:26 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20spring=20=E4=BA=8B=E4=BB=B6=E6=9C=BA=E5=88=B6=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84=20OssConfig=20=E7=BC=93=E5=AD=98=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/core/utils/SpringUtils.java | 9 +++++ .../service/impl/SysOssConfigServiceImpl.java | 34 +++++++++++-------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java index 08e2ff56..7887c2c9 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java @@ -3,6 +3,7 @@ package com.ruoyi.common.core.utils; import cn.hutool.extra.spring.SpringUtil; import org.springframework.aop.framework.AopContext; import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.context.ApplicationContext; /** * spring工具类 @@ -59,4 +60,12 @@ public final class SpringUtils extends SpringUtil { public static T getAopProxy(T invoker) { return (T) AopContext.currentProxy(); } + + /** + * 获取spring上下文 + */ + public static ApplicationContext context() { + return getApplicationContext(); + } + } diff --git a/ruoyi-modules/ruoyi-resource/src/main/java/com/ruoyi/resource/service/impl/SysOssConfigServiceImpl.java b/ruoyi-modules/ruoyi-resource/src/main/java/com/ruoyi/resource/service/impl/SysOssConfigServiceImpl.java index 1178503e..d64635c4 100644 --- a/ruoyi-modules/ruoyi-resource/src/main/java/com/ruoyi/resource/service/impl/SysOssConfigServiceImpl.java +++ b/ruoyi-modules/ruoyi-resource/src/main/java/com/ruoyi/resource/service/impl/SysOssConfigServiceImpl.java @@ -11,6 +11,7 @@ import com.ruoyi.common.core.constant.CacheNames; import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.utils.JsonUtils; +import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.mybatis.core.page.PageQuery; import com.ruoyi.common.mybatis.core.page.TableDataInfo; @@ -25,6 +26,7 @@ import com.ruoyi.resource.mapper.SysOssConfigMapper; import com.ruoyi.resource.service.ISysOssConfigService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -57,7 +59,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService { if ("0".equals(config.getStatus())) { RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey); } - setConfigCache(true, config); + SpringUtils.context().publishEvent(config); } // 初始化OSS工厂 OssFactory.init(); @@ -88,7 +90,11 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService { public Boolean insertByBo(SysOssConfigBo bo) { SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class); validEntityBeforeSave(config); - return setConfigCache(baseMapper.insert(config) > 0, config); + boolean flag = baseMapper.insert(config) > 0; + if (flag) { + SpringUtils.context().publishEvent(config); + } + return flag; } @Override @@ -101,7 +107,11 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService { luw.set(ObjectUtil.isNull(config.getExt1()), SysOssConfig::getExt1, ""); luw.set(ObjectUtil.isNull(config.getRemark()), SysOssConfig::getRemark, ""); luw.eq(SysOssConfig::getOssConfigId, config.getOssConfigId()); - return setConfigCache(baseMapper.update(config, luw) > 0, config); + boolean flag = baseMapper.update(config, luw) > 0; + if (flag) { + SpringUtils.context().publishEvent(config); + } + return flag; } /** @@ -165,19 +175,15 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService { } /** - * 如果操作成功 则更新缓存 + * 更新配置缓存 * - * @param flag 操作状态 * @param config 配置 - * @return 返回操作状态 */ - private boolean setConfigCache(boolean flag, SysOssConfig config) { - if (flag) { - CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); - RedisUtils.publish(OssConstant.DEFAULT_CONFIG_KEY, config.getConfigKey(), msg -> { - log.info("发布刷新OSS配置 => " + msg); - }); - } - return flag; + @EventListener + public void updateConfigCache(SysOssConfig config) { + CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); + RedisUtils.publish(OssConstant.DEFAULT_CONFIG_KEY, config.getConfigKey(), msg -> { + log.info("发布刷新OSS配置 => " + msg); + }); } }