Spring 中获取配置的三种方式
-
通过 @Value 方式动态获取单个配置
-
通过 @ConfigurationProperties + 前缀方式批量获取配置
-
通过 Environment 动态获取单个配置
通过 @Value 动态获取单个配置
-
作用
-
可修饰到任一变量获取,使用较灵活
-
-
优点
-
使用简单,且使用关联的链路较短
-
-
缺点
-
配置名不能被有效枚举到
-
每一个配置的使用都需重新定义,使用较为麻烦
-
项目强依赖配置的定义,配置不存在则会导致项目无法启动
-
-
使用场景
-
项目强依赖该配置的加载,想要从源头避免因配置缺失导致的未知问题
-
只想使用少数几个配置
-
-
配置示例
1.
6. 代码示例
@Value("${demo.name}")
private String name;
@Test
public void getValue(){
System.out.println("name=="+name);
}
通过 @ConfigurationProperties + 前缀方式批量获取
-
作用
-
用于配置类的修饰或批量配置的获取
-
-
优点
-
使用配置只需确定 key 的前缀即能使用,有利于批量获取场景的使用
-
因采用前缀匹配,所以在使用新的相同前缀 key 的配置时无需改动代码
-
-
缺点
-
使用复杂,需定义配置类或者手动创建 bean 后引入使用
-
增加新的前缀相同 key 时可能会引入不稳定因素
-
-
使用场景
-
需要同时使用多前缀相同 key 的配置
-
期望增加新配置但不修改代码的 properties 注入
-
5. 配置示例
6. 代码示例
/**
* @description: 人类
* @author: Mr.Peach
* @create: 2022-03-09 13:36
**/
@Data
@Component
@ConfigurationProperties(prefix = "people")
public class People {
private String name;
private String sex;
private int age;
private List<String> likes;
}
@Autowired
private People people;
@Test
public void getPeople(){
System.out.println("people=="+people.toString());
}
当然也可以通过配置类@Bean中进行配置绑定,我是直接在类上进行的
通过 Environment 动态获取单个配置
-
作用
-
用于动态在程序代码中获取配置,而配置 key 不需提前定义
-
-
优点
-
获取的配置的 key 可不提前定义,程序灵活性高
-
配置 key 可使用枚举统一放置与管理
-
-
缺点
-
使用较复杂,需继承 Environment 接口形成工具类进行获取
-
获取 key 对应的枚举与 key 定义分离,value 获取链路较长
-
-
使用场景
-
只需使用少量的配置
-
获取配置的 key 无提前定义,需要根据对配置的有无进行灵活使用
-
-
配置示例
6. 代码示例
/**
* @description: 环境配置
* @author: Mr.Peach
* @create: 2022-03-09 15:32
**/
@Component
public class ConfigByEnvironment implements EnvironmentAware {
private static final Logger log = LoggerFactory.getLogger(ConfigByEnvironment.class);
private Environment environment;
public Optional<String> get(String configKey){
String config = environment.getProperty(configKey);
return Objects.isNull(config) ? Optional.empty() : Optional.of(config);
}
public void get(String configKey, Consumer<String> consumer){
Optional<String> config = get(configKey);
if(!config.isPresent()){
log.warn("application config, get config by key fail, key: {}", configKey);
}
config.ifPresent(consumer);
}
@Override
public void setEnvironment(@NonNull Environment environment) {
this.environment = environment;
}
/**
* @description: 环境配置key
* @author: Mr.Peach
* @create: 2022-03-09 15:46
**/
public enum ConfigByEnvironmentKey {
MY_CONF("my.conf", "我的配置");
private String key;
private String description;
ConfigByEnvironmentKey(String key, String description) {
this.key = key;
this.description = description;
}
public String getKey() {
return key;
}
public String getDescription() {
return description;
}
}
@Autowired
private ConfigByEnvironment configByEnvironment;
private String value;
@Test
public void getEnvironment(){
Optional<String> optionalS = configByEnvironment.get((ConfigByEnvironmentKey.MY_CONF.getKey()));
System.out.println("optionalS=="+optionalS.get());
configByEnvironment.get(ConfigByEnvironmentKey.MY_CONF.getKey(), (String val)->{
System.out.println("val==="+val);
value=val;
} );
System.out.println("value===="+value);
}
到此结束~~
都是亲自验证过的,可直接使用~
最后截个图总结一下啦