开发者

Can I use ConfigSlurper to reference a config file in the classpath?

I want to use the contents of a config file in several ways, including in integration tes开发者_开发技巧ts and in my BootStrap. If my config file is under src/groovy and is called "com.corp.MyConfig.groovy", what should I pass to the ConfigSlurper parse method?


I guess what happens is that your Groovy file gets compiled and ends up being a class in your binary directory (classpath). Instead of trying to load it via the URL try to load the script class.

Class scriptClass = getClass().classLoader.loadClass('com.corp.MyConfig')
ConfigObject config = new ConfigSlurper().parse(scriptClass)


If your config file is available on the classpath, I would suggest using ClassLoader.getResource() to get it:

URL url = MyClass.class.getClassLoader().getResource("com/corp/MyConfig.groovy");
config = new ConfigSlurper().parse(url);


From a POGO you can also use:

import grails.util.Holders

class Foo {
    def bar() {
        println(Holders.config.grails.serverURL)
    }
}

From: How do I get at the goodies in my Grails Config.groovy at runtime?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜