gpt4 book ai didi

java - 是否可以将 application.yml 中的属性解析为具有不同名称的 java 类字段?

转载 作者:行者123 更新时间:2023-12-02 08:58:56 25 4
gpt4 key购买 nike

是否可以将 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,但我必须保留原始名称 webClient application.yml

可以吗?

附注如果它对解决方案很重要,我正在使用 Spring 框架。

最佳答案

当然有可能。

Spring

当你从yml文件中读取属性时,你只需要提供存储在basePath中的key

@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")

希望能解决问题

关于java - 是否可以将 application.yml 中的属性解析为具有不同名称的 java 类字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60315318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com