有时候我们需要一些全局的常量进行配置,除了定义为全局常量外还可以通过读取配置的方式进行动态修改
application.yaml
jili:
weixin:
appId: wx00001
appSecret: abbcccccc
push:
badge: 1
sound: default
Myproperties.java
package com.jili.redistest.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author: Jili
* @date: Created on 2021/7/12 11:17
*/
@Data
@Component
@ConfigurationProperties(prefix = "jili",ignoreUnknownFields = false)
public class Myproperties {
private final Weixin weixin= new Weixin();
private final Push push = new Push();
@Data
public static class Weixin{
private String appId;
private String appSecret;
}
@Data
public static class Push{
private String badge;
private String sound;
}
}
ignoreUnknownFields = false
告诉 Spring Boot 在有属性不能匹配到声明的域的时候抛出异常prefix
用来选择哪个属性的prefix名字来绑定
test.java
@Test
void test1(){
System.out.println(myproperties.getWeixin().getAppId());
myproperties.getWeixin().setAppId("123456");
System.out.println(myproperties.getWeixin().getAppId());
}
wx00001
123456