第一种:
创建Global 内直接读取,因为是静态方法更好获取
/**
* 当前对象实例
*/
private static Global global = new Global();
/**
* 属性文件加载对象
*/
private static PropertiesLoader loader = new PropertiesLoader("xxx.properties");//xxx.yml
/**
* 获取配置
* @see {fns:getConfig('xxxx')}
*/
public static String getConfig(String key) {
String value = map.get(key);
if (value == null){
value = loader.getProperty(key);
map.put(key, value != null ? value : StringUtils.EMPTY);
}
return value;
}
第二种:
使用注解:@ConfigurationProperties(prefix = "xxx.xxx")
实例如下:
@Component
@Slf4j
@ConfigurationProperties(prefix = "xxx.xxx")
@Data
public class BaiRongInvoker {
private String userName;
private String password;
private String apiCode;
private String loginName;
private String apiName;
}
注意:对应参数名称,要与.yml配置保持一致。spring的版本