diff --git a/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/listener/MenuResourceUpdateListener.java b/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/listener/MenuResourceUpdateListener.java new file mode 100644 index 000000000..cb69e95d8 --- /dev/null +++ b/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/listener/MenuResourceUpdateListener.java @@ -0,0 +1,37 @@ +package cn.stylefeng.roses.kernel.system.modular.menu.listener; + +import cn.hutool.extra.spring.SpringUtil; +import cn.stylefeng.roses.kernel.rule.listener.ApplicationStartedListener; +import cn.stylefeng.roses.kernel.system.modular.menu.service.SysMenuResourceService; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * 监听项目启动,进行menu_resource表的资源名称前缀修改 + * + * @author fengshuonan + * @date 2022/11/16 23:05 + */ +public class MenuResourceUpdateListener extends ApplicationStartedListener implements Ordered { + + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE; + } + + @Override + public void eventCallback(ApplicationStartedEvent event) { + + SysMenuResourceService sysMenuResourceService = SpringUtil.getBean(SysMenuResourceService.class); + + // 获取environment参数 + ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment(); + String springApplicationName = environment.getProperty("spring.application.name"); + + // 获取所有menu_resource表记录 + sysMenuResourceService.updateNewAppCode(springApplicationName); + + } + +} diff --git a/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/SysMenuResourceService.java b/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/SysMenuResourceService.java index a9ec2b831..2c8f8194c 100644 --- a/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/SysMenuResourceService.java +++ b/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/SysMenuResourceService.java @@ -57,4 +57,12 @@ public interface SysMenuResourceService extends IService { */ void addMenuResourceBind(SysMenuResourceRequest sysMenuResourceRequest); + /** + * 更新本表的所有资源编码,改为新的应用code前缀 + * + * @author fengshuonan + * @date 2022/11/16 23:13 + */ + void updateNewAppCode(String newAppCode); + } diff --git a/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/impl/SysMenuResourceServiceImpl.java b/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/impl/SysMenuResourceServiceImpl.java index e38c589d9..7fcdbeebc 100644 --- a/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/impl/SysMenuResourceServiceImpl.java +++ b/kernel-s-system/system-business-menu/src/main/java/cn/stylefeng/roses/kernel/system/modular/menu/service/impl/SysMenuResourceServiceImpl.java @@ -25,6 +25,7 @@ package cn.stylefeng.roses.kernel.system.modular.menu.service.impl; import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.rule.util.GunsResourceCodeUtil; import cn.stylefeng.roses.kernel.system.api.pojo.menu.SysMenuResourceRequest; import cn.stylefeng.roses.kernel.system.modular.menu.entity.SysMenuResource; import cn.stylefeng.roses.kernel.system.modular.menu.mapper.SysMenuResourceMapper; @@ -91,4 +92,20 @@ public class SysMenuResourceServiceImpl extends ServiceImpl list = this.list(); + + // 批量更新资源编码 + for (SysMenuResource sysMenuResource : list) { + String newResourceCode = GunsResourceCodeUtil.replace(sysMenuResource.getResourceCode(), newAppCode); + sysMenuResource.setResourceCode(newResourceCode); + } + + this.updateBatchById(list); + + } + } diff --git a/kernel-s-system/system-spring-boot-starter/src/main/resources/META-INF/spring.factories b/kernel-s-system/system-spring-boot-starter/src/main/resources/META-INF/spring.factories index f19bacc68..f24fcb2ef 100644 --- a/kernel-s-system/system-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/kernel-s-system/system-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -4,3 +4,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ cn.stylefeng.roses.kernel.system.starter.cache.GunsResourceMemoryCacheAutoConfiguration,\ cn.stylefeng.roses.kernel.system.starter.cache.GunsResourceRedisCacheAutoConfiguration,\ cn.stylefeng.roses.kernel.system.starter.GunsSystemHomeStatisticsAutoConfiguration +org.springframework.context.ApplicationListener=\ + cn.stylefeng.roses.kernel.system.modular.menu.listener.MenuResourceUpdateListener \ No newline at end of file