diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/spring/SpringUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/spring/SpringUtils.java index ebc6d27f..8b37e416 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/spring/SpringUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/spring/SpringUtils.java @@ -3,6 +3,7 @@ package com.ruoyi.common.utils.spring; import cn.hutool.extra.spring.SpringUtil; import org.springframework.aop.framework.AopContext; import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; /** @@ -62,4 +63,12 @@ public final class SpringUtils extends SpringUtil { return (T) AopContext.currentProxy(); } + + /** + * 获取spring上下文 + */ + public static ApplicationContext context() { + return getApplicationContext(); + } + } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java index 8ceff7cb..d6269242 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java @@ -16,6 +16,7 @@ import com.ruoyi.common.utils.JsonUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.redis.CacheUtils; import com.ruoyi.common.utils.redis.RedisUtils; +import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.oss.constant.OssConstant; import com.ruoyi.oss.factory.OssFactory; import com.ruoyi.system.domain.SysOssConfig; @@ -25,6 +26,7 @@ import com.ruoyi.system.mapper.SysOssConfigMapper; import com.ruoyi.system.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); + }); } }