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 5d960ab59c227bdb4a5b0159c1c6f36129b08455..54391d512c41e0aa61bbb7315cc312d67c50d17d 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 0000000000000000000000000000000000000000..209eaad1ba651bf43bf6ed49f2f4f7e3c7c435ad --- /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 0000000000000000000000000000000000000000..85c937de76012ade392d456638ea24e6e9ca3907 --- /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 bf65c376a7f800c9c525d7408351d7eb5fa87680..64bba8c6ad9102afdeef210d56f8b7871e368ad1 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 eb32f931972f795890d317d97ad793c85a5a9834..ca5e3b5aca29e16d1c6aa7bc994e1e47428818f2 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