获取yml中配置信息的方式很多,最常见的就是直接利用@Value注解,但是这样不美观,当需要多个配置中的信息时,类中就需要定义多个变量。
像这样:
@Value("${clqbh}") String clqbh; @Value("${overTime}") long overTime; @Value(
"${overTime1}") long overTime1; @Value("${overTime2}") long overTime2; .......
确保只需要一个配置信息的话,可以使用上述方式。多个的话建议使用以下方式:

1. yml配置文件中

按格式写好,名称自定义,我的父级名称是inter。

2. 实现常量配置类
@Configuration @ConfigurationProperties(prefix = "inter") @Data public class
Constant { /** *互联网通道get地址 */ private String channelGetUrl; /**
*内网排队叫号人数获取get地址 */ private String queueNotifyUrl; /**
*同步数据处理(回调接口,通过互联网通道请求的内网接口成功并有数据返回时,会调用此接口通知并返回数据) */ private String pdjhGawUrl
; }
@ConfigurationProperties(prefix = “inter”)是关键,表明获取的是inter下的配置信息,将变量都定义到这个类中。
最后在controller层或者其他注入类中只需要也将Constant类注入即可。
@Autowired private final Constant constant . . . // 如下即可调用该配置信息 constant.
getChannelGetUrl(); constant.getQueueNotifyUrl(); constant.getPdjhGawUrl();
增加了代码的可读性,维护起来也更容易。

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信