From 2f431a592f242b722753329b781abe7212dc3534 Mon Sep 17 00:00:00 2001 From: xclhove <2959169303@qq.com> Date: Sun, 16 Jun 2024 19:24:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(runner):=20=E5=A2=9E=E5=8A=A0=20Abstra?= =?UTF-8?q?ctRunner=20=E7=B1=BB=E4=BB=A5=E6=96=B9=E4=BE=BF=E6=89=A9?= =?UTF-8?q?=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 增加 AbstractRunner 类,springboot启动后会执行AbstractRunner子类的duRun方法 2. 重构以前的Running类以继承AbstractRunner --- .../xclhove/xnote/runner/AbstractRunner.java | 44 +++++++++++++++++ .../com/xclhove/xnote/runner/MinioRunner.java | 25 ++++++---- .../{XnoteRunner.java => NoteRunner.java} | 47 ++++++++++++++----- .../com/xclhove/xnote/runner/RedisRunner.java | 29 ++++++++---- 4 files changed, 118 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/xclhove/xnote/runner/AbstractRunner.java rename src/main/java/com/xclhove/xnote/runner/{XnoteRunner.java => NoteRunner.java} (63%) diff --git a/src/main/java/com/xclhove/xnote/runner/AbstractRunner.java b/src/main/java/com/xclhove/xnote/runner/AbstractRunner.java new file mode 100644 index 0000000..a0b9833 --- /dev/null +++ b/src/main/java/com/xclhove/xnote/runner/AbstractRunner.java @@ -0,0 +1,44 @@ +package com.xclhove.xnote.runner; + +import com.xclhove.xnote.config.RunnerConfig; +import com.xclhove.xnote.util.SubclassFinder; +import lombok.Data; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Set; + +/** + * @author xclhove + */ +@Component +@Data +public abstract class AbstractRunner implements ApplicationRunner { + @Resource + private final ApplicationContext applicationContext; + @Resource + protected RunnerConfig runnerConfig; + + @Override + public void run(ApplicationArguments args) { + // 获取AbstractRunner的子类 + Set subclasses = SubclassFinder.findSubclasses(AbstractRunner.class); + + // 执行AbstractRunner子类的doRun方法 + subclasses.forEach(beanDefinition -> { + try { + Class aClass = Class.forName(beanDefinition.getBeanClassName()); + AbstractRunner runner = (AbstractRunner) applicationContext.getBean(aClass); + runner.doRun(args); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + public abstract void doRun(ApplicationArguments args); +} diff --git a/src/main/java/com/xclhove/xnote/runner/MinioRunner.java b/src/main/java/com/xclhove/xnote/runner/MinioRunner.java index c29780e..b91d465 100644 --- a/src/main/java/com/xclhove/xnote/runner/MinioRunner.java +++ b/src/main/java/com/xclhove/xnote/runner/MinioRunner.java @@ -1,36 +1,45 @@ package com.xclhove.xnote.runner; -import com.xclhove.xnote.config.MinioConfig; import com.xclhove.xnote.tool.MinioTool; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; +import javax.annotation.Resource; + /** * 启动时检查minio是否连接成功 * * @author xclhove */ @Component -@RequiredArgsConstructor @Slf4j -public class MinioRunner implements ApplicationRunner { - private final MinioTool minioTool; +public class MinioRunner extends AbstractRunner { + @Resource + private MinioTool minioTool; + + public MinioRunner(ApplicationContext applicationContext) { + super(applicationContext); + } @Override - public void run(ApplicationArguments args) { + public void doRun(ApplicationArguments args) { checkMinio(); } /** * 检查minio是否连接成功 */ - public void checkMinio() { + private void checkMinio() { + if (!runnerConfig.getEnableMinioStatusCheckRunner()) { + return; + } + try { boolean bucketExist = minioTool.bucketExist(); if (bucketExist) { + log.info("minio连接成功!"); return; } diff --git a/src/main/java/com/xclhove/xnote/runner/XnoteRunner.java b/src/main/java/com/xclhove/xnote/runner/NoteRunner.java similarity index 63% rename from src/main/java/com/xclhove/xnote/runner/XnoteRunner.java rename to src/main/java/com/xclhove/xnote/runner/NoteRunner.java index 4ff69af..5fe8763 100644 --- a/src/main/java/com/xclhove/xnote/runner/XnoteRunner.java +++ b/src/main/java/com/xclhove/xnote/runner/NoteRunner.java @@ -5,13 +5,13 @@ import com.xclhove.xnote.entity.attribute.NoteKeyword; import com.xclhove.xnote.entity.table.Note; import com.xclhove.xnote.service.NoteService; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; @@ -21,20 +21,29 @@ * @author xclhove */ @Component -@RequiredArgsConstructor @Slf4j -public class XnoteRunner implements ApplicationRunner { - private final NoteService noteService; - @Value("${xnote.keywords.convert-enable: false}") - private boolean enableKeywordsConvert; +@Order +public class NoteRunner extends AbstractRunner { + @Resource + private NoteService noteService; + + public NoteRunner(ApplicationContext applicationContext) { + super(applicationContext); + } @Override - public void run(ApplicationArguments args) { + public void doRun(ApplicationArguments args) { convertKeywords(); + replaceImageUrl(); } + /** + * 转换关键词的存储结构 + */ private void convertKeywords() { - if (!enableKeywordsConvert) return; + if (!runnerConfig.getEnableKeywordsConvertRunner()) { + return; + } try { log.info("关键词转化……"); @@ -68,8 +77,24 @@ private void convertKeywords() { log.info("关键词转化已经完成!"); System.exit(0); } catch (Exception e) { - log.info("无需转化!"); + log.info("关键词无需转化!"); + } + } + + /** + * 替换笔记中的图片地址 + */ + private void replaceImageUrl() { + if (!runnerConfig.getEnableImageUrlReplaceRunner()) { return; } + + List list = new ArrayList<>(); + noteService.list().forEach(note -> { + note.setContent(note.getContent().replaceAll("xnote.xclhove.top/api", "api.xclhove.top/xnote")); + list.add(note); + }); + noteService.updateBatchById(list); + log.info("替换图片地址成功!"); } } diff --git a/src/main/java/com/xclhove/xnote/runner/RedisRunner.java b/src/main/java/com/xclhove/xnote/runner/RedisRunner.java index 3af1662..dd2af93 100644 --- a/src/main/java/com/xclhove/xnote/runner/RedisRunner.java +++ b/src/main/java/com/xclhove/xnote/runner/RedisRunner.java @@ -1,35 +1,48 @@ package com.xclhove.xnote.runner; import com.xclhove.xnote.tool.RedisTool; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; +import javax.annotation.Resource; + /** * 启动时检查redis是否连接成功 * * @author xclhove */ @Component -@RequiredArgsConstructor @Slf4j -public class RedisRunner implements ApplicationRunner { - private final RedisTool redisTool; +@Order(1) +public class RedisRunner extends AbstractRunner { + @Resource + private RedisTool redisTool; + + public RedisRunner(ApplicationContext applicationContext) { + super(applicationContext); + } @Override - public void run(ApplicationArguments args) { - checkRedis(); + public void doRun(ApplicationArguments args) { + checkRedisStatus(); } /** * 检查redis是否连接成功 */ - public void checkRedis() { + private void checkRedisStatus() { + if (!runnerConfig.getEnableRedisStatusCheckRunner()) { + return; + } + if (!redisTool.connected()) { log.error("redis连接失败!请检查配置文件和redis是否启动。"); System.exit(1); + return; } + log.info("redis连接成功!"); } }