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

荒凉一梦 提交于 2019-11-30 14:14:23

问题


How to update external config files (e.g.: config-ex.groovy, config-ex.properties) without rebuilding the war file in Grails?

Restarting the application server will apply the new updates from external config files.


回答1:


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




回答2:


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.




回答3:


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))
 }


来源:https://stackoverflow.com/questions/13561127/how-to-update-external-config-files-without-rebuilding-war-file-in-grails

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