简体   繁体   English

Groovy configslurper直接导入对象

[英]Groovy configslurper directly into object

I have a groovy configuration file that I'm parsing with ConfigSlurper. 我有一个用ConfigSlurper解析的常规配置文件。 This gives me a ConfigObject that functions like a map. 这为我提供了一个ConfigObject,其功能类似于地图。

String x = "foo"
String y = "bar"

I also have an object that exactly matches the Configuration file 我还有一个与配置文件完全匹配的对象

public class Example{
String x
String y
}

Now I could write code to load the Config file I just slurped into this object, but does Groovy have an automatic way of doing it? 现在,我可以编写代码以加载刚放入该对象中的Config文件,但是Groovy是否有自动的方法呢? Like a 像一个

`Example e = ConfigObject.parseIntoObject(Example.class);`

You can do it this way, for example in your config file 您可以通过这种方式进行操作,例如在配置文件中

conf {
    example = new Example(
                  x: "foo",
                  y: "bar"
              )
}

or 要么

conf {
      example = {
          x: "foo",
          y: "bar"
      } as Example
}

and read this 并阅读此

Example e = (Example) new ConfigSlurper().parse(new URL("file:///...path").conf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM