3.4 外部配置 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
3.4 外部配置
Some deployments require that configuration be sourced from more than one place and be changeable without requiring a rebuild of the application. In order to support deployment scenarios such as these the configuration can be externalized. To do so, point Grails at the locations of the configuration files that should be used by adding a In the above example we're loading configuration files (both Java Properties files and ConfigSlurper configurations) from different places on the classpath and files located in This can be useful in situations where the config is either coming from a plugin or some other part of your application. A typical use for this is re-using configuration provided by plugins across multiple applications.Ultimately all configuration files get merged into the
某些部署要求配置信息可以放在多个源文件中,并且在不需要重现编译打包的情况下可被修改。为了能够支撑这些特定的部署场景,Grails的配置信息可以在外部进行配置。要完成此功能,只需要设置grails.config.locations setting in Config.groovy, for example:grails.config.locations = [
"classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy" ]USER_HOME.It is also possible to load config by specifying a class that is a config script.grails.config.locations = [com.my.app.MyConfig]
config property of the GrailsApplication object and are hence obtainable from there.Values that have the same name as previously defined values will overwrite the existing values, and the pointed to configuration sources are loaded in the order in which they are defined.
Config.groovy中的grails.config.locations的值来指向配置文件的位置即可,比如:grails.config.locations = [
"classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy" ]USER_HOME下加载不同的配置文件(包括Java属性文件和Groovy的ConfigSlurper也可以通过配置脚本的类名来加载,比如:grails.config.locations = [com.my.app.MyConfig]
config属性中,因此,要获取使用也是通过它来实现的。配置中如果一个名字有多个值,那么新值将覆盖旧的,其顺序是根据源文件的加载顺序来定义的。Config Defaults
The configuration values contained in the locations described by thegrails.config.locations property will override any values defined in your application Config.groovy file which may not be what you want. You may want to have a set of default values be be loaded that can be overridden in either your application's Config.groovy file or in a named config location. For this you can use the grails.config.defaults.locations property.This property supports the same values as the grails.config.locations property (i.e. paths to config scripts, property files or classes), but the config described by grails.config.defaults.locations will be loaded before all other values and can therefore be overridden. Some plugins use this mechanism to supply one or more sets of default configuration that you can choose to include in your application config.Grails also supports the concept of property place holders and property override configurers as defined in Spring For more information on these see the section on Grails and Spring
缺省配置
通过grails.config.locations定义的配置将 优先于 你在Config.groovy文件中的任意值,这也许并非你所需要的。你希望Config.groovy或者特定位置的文件才能加载并且重载特定的 缺省值 ,这种情况,你可以通过配置grails.config.defaults.locations属性来实现。此属性的值跟grails.config.locations一样(可以是特定路径下的配置脚本、属性文件或者类名),但与之不同的是通过grails.config.defaults.locations来配置的值比其他方式加载的 更早 ,因此也就可有被重载。有些插件就是通过此机制来实现一个或者多个缺省配置的。Grails还支撑Spring 配置中属性占位符和覆盖的概念。更多信息请参考Grails和Spring章节

