Externalized Configuration 中的第 24.3 节表示 .properties
或 .yml
文件可用于外部配置,但我希望我的外部配置为 .groovy
文件就像我的 application.groovy
我已经从 .yml
转换的文件.我怎样才能做到这一点?
Grails 版本 3.2.0.M2
更新:
我能够根据@Michal_Szulc 提供的答案完成这项工作
请注意 ConfigSlurper
需要当前环境才能正常工作。另请注意,这些更改将对 my_grails_app/grails-app/init/my_grails_app/Application.groovy
进行。文件,而不是 my_grails_app/grails-app/conf/application.groovy
如果您从 .yml
转换而来,您可能会拥有该文件配置到 .groovy
配置。
package my_grails_app
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.context.EnvironmentAware
import org.springframework.core.env.Environment
import org.springframework.core.env.MapPropertySource
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main( String[] args ) {
GrailsApp.run( Application, args )
}
@Override
void setEnvironment( Environment environment ) {
def appName = grails.util.Metadata.current.getApplicationName()
// The value of this environment variable should be the absolute path to an external .groovy config file like /opt/my_grails_app/my_grails_app-config.groovy.
def envVarName = "MY_GRAILS_APP_CONFIG"
// If you don't want to use an environment variable you can specify your external .groovy config file here, but the environment variable will still be checked first.
def dfltAppConfigFileName = "/opt/${ appName }/${ appName }-config.groovy"
def appConfigFile = null
def loadDfltConfig = false
// Try to load config specified by the environment variable first
println "Checking the environment variable ${ envVarName } for the configuration file location..."
def envVarVal = System.getenv( envVarName ) ?: System.getProperty( envVarName )
if( envVarVal ) {
appConfigFile = new File( envVarVal )
if( !appConfigFile.exists() ) {
println "The configuration file ${ appConfigFile } specified by the environment variable ${ envVarName } does not exist. Checking for the default configuration file ${ dfltAppConfigFileName } instead..."
appConfigFile = null
loadDfltConfig = true
}
} else {
println "The environment variable ${ envVarName } which specifies the configuration file to be loaded does not exist. Checking for the default configuration file ${ dfltAppConfigFileName } instead..."
appConfigFile = null
loadDfltConfig = true
}
// Try loading the default config file since we couldn't find one specified by the environment variable
if( loadDfltConfig ) {
appConfigFile = new File( dfltAppConfigFileName )
if( !appConfigFile.exists() ) {
println "The default configuration file ${ dfltAppConfigFileName } does not exist."
appConfigFile = null
}
}
// Load the config file if it exists, otherwise exit
if( appConfigFile ) {
println "Loading configuration file ${ appConfigFile }"
def config = new ConfigSlurper( environment.activeProfiles.first() ).parse( appConfigFile.toURI().toURL() )
environment.propertySources.addFirst( new MapPropertySource( "${ appName }-config", config ) )
} else {
println "No configuration file found. Exiting."
System.exit( 1 )
}
最佳答案
我找到了 this thread和报价格雷姆·罗彻 :
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 :
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 - 如何在 Grails 3 中使用外部 .groovy 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38645019/