提交 679eb5f7 编写于 作者: 无难事者若执's avatar 无难事者若执

feat : id生成框架: 支持application.yaml配置默认id生成算法

上级 6fb269d7
...@@ -71,4 +71,13 @@ public class A{ ...@@ -71,4 +71,13 @@ public class A{
return "uuid"; return "uuid";
} }
} }
```
4.1 使用`application.yml` 修改默认的id生成器
```yaml
spring:
utils:
id:
defaultIdGenerator: 'uuid' // 修改
``` ```
\ No newline at end of file
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 {
}
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";
}
}
package com.kx.utils.id; package com.kx.utils.id;
import com.kx.config.KxUtilsConfigurationProperties;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -13,6 +14,8 @@ import java.util.Set; ...@@ -13,6 +14,8 @@ import java.util.Set;
public class IdService { public class IdService {
@Autowired @Autowired
private IdAlgorithmRegister idAlgorithmRegister; private IdAlgorithmRegister idAlgorithmRegister;
@Autowired
private KxUtilsConfigurationProperties kxUtilsConfigurationProperties;
/** /**
* 生成指定算法的id * 生成指定算法的id
...@@ -27,7 +30,7 @@ public class IdService { ...@@ -27,7 +30,7 @@ public class IdService {
* @return id * @return id
*/ */
public String generate(){ public String generate(){
return idAlgorithmRegister.getAlgorithm("uuid").generate(); return idAlgorithmRegister.getAlgorithm(kxUtilsConfigurationProperties.getId().getDefaultIdGenerator()).generate();
} }
/** /**
......
# Auto config # Auto config
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kx.config.ConfigPropertiesAutoConfiguration,\
com.kx.config.IdAutoConfiguration com.kx.config.IdAutoConfiguration
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册