From 679eb5f7a73ed5f9ab9857f05ad9c9c0bdf45a8b Mon Sep 17 00:00:00 2001 From: kongxiang <316378085@qq.com> Date: Sat, 26 Aug 2023 21:08:36 +0800 Subject: [PATCH] =?UTF-8?q?feat=20:=20id=E7=94=9F=E6=88=90=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=EF=BC=9A=20=E6=94=AF=E6=8C=81application.yaml?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=BB=98=E8=AE=A4id=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md" | 9 +++++++ .../ConfigPropertiesAutoConfiguration.java | 15 +++++++++++ .../KxUtilsConfigurationProperties.java | 25 +++++++++++++++++++ src/main/java/com/kx/utils/id/IdService.java | 5 +++- src/main/resources/META-INF/spring.factories | 1 + 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/kx/config/ConfigPropertiesAutoConfiguration.java create mode 100644 src/main/java/com/kx/config/KxUtilsConfigurationProperties.java diff --git "a/doc/\351\200\232\347\224\250id\347\224\237\346\210\220\345\231\250\351\242\206\345\237\237/README.md" "b/doc/\351\200\232\347\224\250id\347\224\237\346\210\220\345\231\250\351\242\206\345\237\237/README.md" index 5d960ab..54391d5 100644 --- "a/doc/\351\200\232\347\224\250id\347\224\237\346\210\220\345\231\250\351\242\206\345\237\237/README.md" +++ "b/doc/\351\200\232\347\224\250id\347\224\237\346\210\220\345\231\250\351\242\206\345\237\237/README.md" @@ -71,4 +71,13 @@ public class A{ return "uuid"; } } +``` + +4.1 使用`application.yml` 修改默认的id生成器 +```yaml +spring: + utils: + id: + defaultIdGenerator: 'uuid' // 修改 + ``` \ No newline at end of file diff --git a/src/main/java/com/kx/config/ConfigPropertiesAutoConfiguration.java b/src/main/java/com/kx/config/ConfigPropertiesAutoConfiguration.java new file mode 100644 index 0000000..209eaad --- /dev/null +++ b/src/main/java/com/kx/config/ConfigPropertiesAutoConfiguration.java @@ -0,0 +1,15 @@ +package com.kx.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * 自动加载 配置文件属性 的自动配置类 + * @author kongxiang + */ +@Configuration(proxyBeanMethods = false) +@EnableConfigurationProperties({KxUtilsConfigurationProperties.class}) +public class ConfigPropertiesAutoConfiguration { + +} diff --git a/src/main/java/com/kx/config/KxUtilsConfigurationProperties.java b/src/main/java/com/kx/config/KxUtilsConfigurationProperties.java new file mode 100644 index 0000000..85c937d --- /dev/null +++ b/src/main/java/com/kx/config/KxUtilsConfigurationProperties.java @@ -0,0 +1,25 @@ +package com.kx.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * utils 项目配置application.yaml配置项 + * @author kongxiang + */ +@ConfigurationProperties(prefix = "spring.utils" ) +@Data +public class KxUtilsConfigurationProperties { + + private IdConfig id = new IdConfig(); + + + @Data + public static class IdConfig { + /** + * 默认id生成器算法 + */ + String defaultIdGenerator = "uuid"; + } + +} diff --git a/src/main/java/com/kx/utils/id/IdService.java b/src/main/java/com/kx/utils/id/IdService.java index bf65c37..64bba8c 100644 --- a/src/main/java/com/kx/utils/id/IdService.java +++ b/src/main/java/com/kx/utils/id/IdService.java @@ -1,5 +1,6 @@ package com.kx.utils.id; +import com.kx.config.KxUtilsConfigurationProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -13,6 +14,8 @@ import java.util.Set; public class IdService { @Autowired private IdAlgorithmRegister idAlgorithmRegister; + @Autowired + private KxUtilsConfigurationProperties kxUtilsConfigurationProperties; /** * 生成指定算法的id @@ -27,7 +30,7 @@ public class IdService { * @return id */ public String generate(){ - return idAlgorithmRegister.getAlgorithm("uuid").generate(); + return idAlgorithmRegister.getAlgorithm(kxUtilsConfigurationProperties.getId().getDefaultIdGenerator()).generate(); } /** diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index eb32f93..ca5e3b5 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,4 +1,5 @@ # Auto config org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.kx.config.ConfigPropertiesAutoConfiguration,\ com.kx.config.IdAutoConfiguration \ No newline at end of file -- GitLab