Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Grails 3中使用外部.groovy配置文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Externalized Configuration中的第24.3节表示.properties或.yml文件可用于外部配置,但我希望我的外部配置是.groovy文件,就像我已经从.yml转换的application.groovy文件一样.我怎样才能做到这一点?

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文件,如果您从a转换了该文件. 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和Graeme Rocher的引文:

和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配置文件全部内容,希望文章能够帮你解决如何在Grails 3中使用外部.groovy配置文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。