![](/img/trans.png)
[英]How to match a class property from application.yml to the java class with a different class name?
[英]Is it possible to parse a property from application.yml to the java class field with a different name?
是否可以将 application.yml 中的属性解析为具有不同名称的 java 类字段?
假设我有以下application.yml
文件
service:
cms:
webClient: http://www.example.com
我的java类是
@Getter
@Setter
@ConfigurationProperties(prefix = "service.cms")
public class CmsProperties {
private String webClient;
}
我想将 java 字段名称CmsProperties#webClient
重命名为CmsProperties#basePath
,但我必须在application.yml
保留原始名称webClient
。
有可能做到吗?
PS 如果对解决方案很重要,我正在使用 Spring Framework。
当然有可能。
春天
当您从 yml 文件读取属性时,您只需要在它存储在 basePath 时提供密钥
@Value("${webClient}")
private String basePath;
普通Java
InputStream input = new FileInputStream("path/to/config.properties")) {
Properties prop = new Properties();
prop.load(input);
String basePath = prop.getProperty("webClient")
希望确实解决答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.