两种属性赋值方式如下,直接上代码:
一种:直接static静态方法直接给static final map赋值;另外一种通过set方法调用静态方法,将yaml中的配置获取到处理完给static final map赋值
注意:
方法二中需要用到@PostConstruct注解给静态变量赋配置文件中的值。
package cn.piesat.bussiness.manage.config;
import cn.piesat.bussiness.manage.constant.Constants;
import cn.piesat.bussiness.manage.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
/**
* @BelongsProject: project-boot
* @BelongsPackage: cn.piesat.bussiness.manage.config
* @Author: ml
* @CreateTime: 2023-04-10 15:00
* @Description: 在线引接地址配置类
* @Version: 1.0
*/
@Configuration
public class OnlineReceiveConfig {
@Value("${online.receive.reqUrls}")
public String onlineReceiveReqUrls;
public static String onlineReceiveReqUrlsLocal;
/**
* 在线引接地址配置
**/
public static final Map<String, String> onlineReceiveUrlsMap = new HashMap<>();
@PostConstruct
public void setOnlineReceiveReqUrlsLocal() {
onlineReceiveReqUrlsLocal = this.onlineReceiveReqUrls;
dealUrls();
}
// static {
// dealUrls();
// System.out.println("test"+onlineReceiveUrlsMap.toString());
onlineReceiveUrlsMap.put("XXX1数据在线接入", "http://localhost:18080/getMBInfo");
onlineReceiveUrlsMap.put("XXX2数据在线接入", "http://localhost:18080/getMBInfo");
onlineReceiveUrlsMap.put("XXX3数据在线接入", "http://localhost:18080/getMBInfo");
// }
public static void dealUrls(){
System.out.println("url:"+onlineReceiveReqUrlsLocal);
if(!onlineReceiveReqUrlsLocal.contains("<>")){
return;
}
String[] urlAllArr = onlineReceiveReqUrlsLocal.split(";");
if(urlAllArr != null && urlAllArr.length > 0){
for (String urls : urlAllArr) {
if(StringUtils.isEmpty(urls) || urls.contains("<>")){
String[] urlsArr = urls.split("<>");
onlineReceiveUrlsMap.put(urlsArr[0], urlsArr[1]);
}
}
}
System.out.println("map:"+onlineReceiveUrlsMap.toString());
}
}
# 在线引接地址配置
online:
receive:
reqUrls: 'XXX1数据在线接入<>http://localhost:18080/getMBInfo;XXX2数据在线接入<>http://localhost:18080/getMBInfo;XXX3数据在线接入<>http://localhost:18080/getMBInfo;'
演示结果如下:
每日一语:但行好事,莫问前程。