Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Groovy配置文件中的继承大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要通过ConfigSlurper定义和读取Groovy配置文件中的几个属性,这些属性将共享一些公共字段并仅添加一个特定字段.像这样的东西:

config {
  // this is something like abstract property
  common {
    field1 = 'value1'
    field2 = 'value2'
  }

  property1 {
    // include fields from common here
    customField = 'prop1value'
  }

  property2 {
    // include fields from common here
    customField = 'prop2value'
  }
}

我很好奇是否有可能以某种方式实现这一目标.因为我对Groovy不是很熟悉,所以我目前的解决方案并不理想,我会说:

config {
  common {
    field1 = 'value1'
    field2 = 'value2'
  }

  property1 = common.clone()
  property1 {
    customField = 'value'
  }

  property2 = common.clone()
  property2 {
    customField = 'value'
  }
}
config.remove('common')

谢谢你的建议

解决方法

你可以做:

config {
    // A common map of values
    def common = [
        field1: 'value1',field2: 'value2'
    ] as ConfigObject

    property1 {
        customField = 'value'
    }

    property2 {
        customField = 'value'
    }

    property1.merge(common)
    property2.merge(common)
}

这是你的意思吗?

大佬总结

以上是大佬教程为你收集整理的Groovy配置文件中的继承全部内容,希望文章能够帮你解决Groovy配置文件中的继承所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。