有时候我们需要一些全局的常量进行配置,除了定义为全局常量外还可以通过读取配置的方式进行动态修改
application.yaml
jili:weixin:appId: wx00001appSecret: abbccccccpush:badge: 1sound: 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();@Datapublic static class Weixin{private String appId;private String appSecret;}@Datapublic static class Push{private String badge;private String sound;}}
ignoreUnknownFields = false告诉 Spring Boot 在有属性不能匹配到声明的域的时候抛出异常prefix用来选择哪个属性的prefix名字来绑定
test.java
@Testvoid test1(){System.out.println(myproperties.getWeixin().getAppId());myproperties.getWeixin().setAppId("123456");System.out.println(myproperties.getWeixin().getAppId());}
wx00001123456
