How to create ConfigObject using only nested maps in Grails?

萝らか妹 提交于 2019-12-11 08:15:46

问题


It is possible to implement closure-based config as a map of maps?

grails {
  acme {
     host = 'localhost'
     poolSettings {
        timeout = 5000
     }
  }
}

The above config is convention for a grails plugin. We're doing a migration, and due to legacy constraints, we need to create the config dynamically to eliminate impact to legacy code.

I started with a simple config:

grails.acme = [host:'localhost']

This works fine with the plugin on startup, so I added a nested map:

grails.acme = [host:'localhost']
def poolProps = //do some work to get pool settings
grails.acme << [poolSettings:poolProps]

On startup I pretty print grails.acme:

{
    "host": "locahost",
    "poolSettings": {
        "timeout": 5000
    }
}

It looks normal, but soon after the acme plugin fails with the following error:

2014-09-27 23:27:07,460 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: No signature of method: AcmePlugin$_closure2_closure5_closure9 .doCall() is applicable for argument types: (grails.spring.BeanBuilder) values: [grails.spring.BeanBuilder@7b067fd7] Possible solutions: doCall(java.lang.Object, java.lang.Object), call(), call([Ljava.lang.Object;), call(java.lang.Object), call(java.lang.Object, java.lang.Object), findAll() Message: No signature of method: AcmeGrailsPlugin$_closure2_closure5_closure9.doCall() is applicable for argument types: (grails.spring.BeanBuilder) values: [grails.spring.BeanBuilder@7b067 fd7] Possible solutions: doCall(java.lang.Object, java.lang.Object), call(), call([Ljava.lang.Object;), call(java.lang.Object), call(java.lang.Object, java.lang.Object)

I'm sort of stumped on this, I'm assuming it's not possible, and I'm not sure how to even approach the closure-based convention dynamically.


回答1:


One problem with your code might be that the config node structure looks perfectly all right, but in fact isn't. I.e. that some nodes are Map instances, but not ConfigObject instances.

In most cases this probably won't matter, but in your case it seems that a 3rd party plugin fails with some ugly and incomprehensible exception.

I'm unable to recreate your problem, so I can't say for sure whether this is the real problem that you've encountered, nor whether the suggested solution fix your problem. Nevertheless, my first attempt would be to fix the types so that all nodes in the configuration tree are of type ConfigObject. Something like this:

def poolProps = [timeout: 5000] as ConfigObject // convert type to ConfigObject
grails.acme.host = 'localhost'
grails.acme << [ poolSettings : poolProps ]

Note that if the maps you create (poolProps in this case) contains nested map elements, you would have to convert those as well.




回答2:


Yes it is possible, actually

grails {
   acme {
      host = 'localhost'
      poolSettings {
         timeout = 5000
      }
   }
}

is the same as:

grails.acme.host = 'localhost'
grails.acme.poolSettings.timeout = 5000

the closures are just syntactic sugar. You can even combine both types:

grails.acme {
   host = 'localhost'
   poolSettings.timeout = 5000
}


来源:https://stackoverflow.com/questions/26081477/how-to-create-configobject-using-only-nested-maps-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!