Java 覆盖application.yml文件spring boot中的默认值
我有下面的配置文件。如果你看下面的文件,我有 默认情况下,android和ios都需要读取这些值 基于设备。对于ios,我们有ios.actionLink.target名称不同。所以我想 仅重写该值(表示替换-LINK,而不是“LOST-DEEPLINK”) 其余的值应该是相同的。类似地,如果你有android系统,也可以这样说 任何与默认值不同的值,则应重写并静止 应该保持不变(在本例中为android.actionSwitch.method)。请一些机构提供解决方案或样本代码Java 覆盖application.yml文件spring boot中的默认值,java,spring-boot,Java,Spring Boot,我有下面的配置文件。如果你看下面的文件,我有 默认情况下,android和ios都需要读取这些值 基于设备。对于ios,我们有ios.actionLink.target名称不同。所以我想 仅重写该值(表示替换-LINK,而不是“LOST-DEEPLINK”) 其余的值应该是相同的。类似地,如果你有android系统,也可以这样说 任何与默认值不同的值,则应重写并静止 应该保持不变(在本例中为android.actionSwitch.method)。请一些机构提供解决方案或样本代码
prop:
responseConfig:
default:
label1: 'label1'
actionSwitch:
target: 'some url'
method: 'PUT'
errorResponse: 'Its an error message'
actionLink:
target: 'REPLACE-LINK'
data:
EXTRA_UUID: '{Id}'
ios:
actionLink:
target: 'LOST-DEEPLINK'
android:
actionSwitch::
method: 'GET'
private String target;
private Map<String, String> data;
private String method;
private String cin;
private String errorResponse;
prop:
响应图:
违约:
label1:“label1”
动作开关:
目标:“某个url”
方法:“放”
errorResponse:“这是一条错误消息”
actionLink:
目标:“替换链接”
数据:
额外_UUID:“{Id}”
网间网操作系统:
actionLink:
目标:“迷失链接”
安卓:
动作开关::
方法:“获取”
私有字符串目标;
私有地图数据;
私有字符串方法;
私有字符串;
私有字符串错误响应;
实际上,您可以通过以下方式定义配置来解决此问题:
prop:
responseConfig:
ios: true
default:
label1: 'label1'
actionSwitch:
target: 'some url'
method: 'PUT'
errorResponse: 'Its an error message'
actionLink:
target: 'REPLACE-LINK'
data:
EXTRA_UUID: '{Id}'
ios:
actionLink:
target: 'LOST-DEEPLINK'
android:
actionSwitch::
method: 'GET'
然后在配置中,您可以依赖这些变量:
@Component
@ConfigurationProperties("prop.responseConfig.default")
public class Config{
@Value("${prop.responseConfig.ios}")
private boolean isIos;
@Value("${ios.actionSwitch.target}")
private String iosActionLinkTarget;
@Value("${android.actionSwitch.method}")
private String androidActionSwitchMethod;
private ActionSwitch actionSwitch;
private ActionLink actionLink;
public ActionLink getActionLink(){
if(isIos){
actionLink.setTarget(iosActionLinkTarget);
}
return this.actionSwitch;
}
public ActionSwitch getActionSwitch(){
if(!isIos){
actionSwitch.setMethod(androidActionSwitchMethod);
}
return this.actionSwitch;
}
@Getter
@Setter
public static ActionSwitch{
private String target;
private String method;
private String errorResponse;
}
@Getter
@Setter
public static ActionLink{
private String target;
private Data data;
}
@Getter
@Setter
public static Data{
private String EXTRA_UUID;
}
}
谢谢你的回复。我使用config server外部化这个yml文件,并使用@ConfigurationProperties(prefix=“prop.responseConfig”)读取这些属性。在这种情况下,我们如何实现上述解决方案?谢谢只是为了澄清SpringCloudConfigServer?如果它是SpringCloudConfigServer,您可以将属性放在src/main/resources/application.yml中,并且在您从ConfigServerYes获取配置后,它将被覆盖!使用SpringCloudConfigServer,它已经被外部化了。它不是我代码库的一部分。我们不希望它成为src/main/resources/的一部分,因为它的值一直在变化,每次我们需要构建和部署时都会发生变化。请测试并让我知道