覆盖application.yml文件spring boot

时间:2018-12-05 23:29:02

标签: java spring-boot

我有以下配置文件。如果你看下面的文件,我有     prop.responseConfig.default,需要同时为android和ios读取这些值                     基于设备。对于ios,我们的ios.actionLink.target名称不同。所以我想                     单独覆盖该值(意味着它应该是“ LOST-DEEPLINK”,而不是“ REPLACE-LINK”)                     其余的值应该相同。同样,如果您有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;                    

1 个答案:

答案 0 :(得分:0)

实际上,您可以通过以下方式定义配置来解决它:

 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;
}

}