如何在Grails 3中使用外部.groovy配置文件 [英] How to use external .groovy config file in Grails 3
问题描述
第24.3节外部化配置表示 .properties
或 .yml
文件可以用于外部配置,但我希望我的外部配置是 .groovy
文件,就像我的 application.groovy code>我已经从
.yml
转换的文件。我怎么能做到这一点?
Section 24.3 in Externalized Configuration indicates a .properties
or .yml
file can be used for external config, but I would like my external config to be a .groovy
file just like my application.groovy
file which I have already converted from .yml
. How can I make this happen?
Grails版本3.2.0.M2
Grails version 3.2.0.M2
更新:
我能够根据@Michal_Szulc提供的答案获得此项工作。
I was able to get this working based on the answer provided by @Michal_Szulc
Note that the ConfigSlurper
needed the current environment to work correctly. Also note that these changes are to be made to the my_grails_app/grails-app/init/my_grails_app/Application.groovy
file, not the my_grails_app/grails-app/conf/application.groovy
file which you may have if you converted from a .yml
configuration to a .groovy
configuration.
推荐答案
我发现此主题
a>和Graeme Rocher的引用
I found this thread and quotation by Graeme Rocher:
Grails 3使用Spring的属性资源概念,因此它将解析
属性从系统,环境和最后的
application.yml / application.groovy
Grails 3 uses Spring's property sources concept, so it will resolve properties from the system, the environment and finally the application.yml/application.groovy
和代码 Clyde Balneaves :
and code by Clyde Balneaves:
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main(String[] args) {
GrailsApp.run(Application)
}
@Override
void setEnvironment(Environment environment) {
//Set up Configuration directory
def krakenHome = System.getenv('KRAKEN_HOME') ?: System.getProperty('KRAKEN_HOME') ?: "/opt/kraken"
println ""
println "Loading configuration from ${krakenHome}"
def appConfigured = new File(krakenHome, 'KrakenConfig.groovy').exists()
println "Loading configuration file ${new File(krakenHome, 'KrakenConfig.groovy')}"
println "Config file found : " + appConfigured
if (appConfigured) {
def config = new ConfigSlurper().parse(new File(krakenHome, 'KrakenConfig.groovy').toURL())
environment.propertySources.addFirst(new MapPropertySource("KrakenConfig", config))
}
}
}
这篇关于如何在Grails 3中使用外部.groovy配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!