How to update external config files without rebuilding war file in Grails

只谈情不闲聊 提交于 2019-11-30 09:58:45

If I understand well you want to externalized Grails config outside the war. You can define an external config in your config.groovy like this

grails.config.locations = ["file:path/to/your/Configfile.groovy"]

See the Grails doc 4.4 Externalized Configuration

Define your external Grails config with:

grails.config.locations = ["file:some/path/to/Config.groovy"]

Then to reload them at runtime, you can use code like this:

def config = grailsApplication.config
def locations = config.grails.config.locations

locations.each {
  String configFileName = it.split('file:')[0]
  config.merge(new ConfigSlurper().parse(new File(configFileName).text))
}

I have the above code in an admin protected Controller.

Went around the houses for this one, thanks Gregg

For services or groovy src files you could use:

import org.springframework.context.ApplicationContext
ApplicationContext ctx = (ApplicationContext) org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
def grailsApplication = ctx.getBean("grailsApplication")
ConfigObject config = ctx.getBean(GrailsApplication).config
def locations = config.grails.config.locations
locations.each {
   String configFileName = it.split("file:")[1]
   config.merge(new ConfigSlurper().parse(new File(configFileName).text))
}

And for abstract classes that are typically extended from controllers:

import grails.util.Holders
def config = Holders.config
def locations = config.grails.config.locations
locations.each {
  String configFileName = it.split("file:")[1]
  config.merge(new ConfigSlurper().parse(new File(configFileName).text))
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!