Reusing Grails variables inside Config.groovy

前端 未结 2 890
逝去的感伤
逝去的感伤 2021-01-13 14:06

In my Config.groovy I have:

// Lots of other stuff up here...

environments {
    development {
        myapp.port = 7500
    }
    production {         


        
相关标签:
2条回答
  • 2021-01-13 14:40

    You could probably get away with this if myapp.port were not in an environments block but that's a side effect of the way Config.groovy is processed rather than being intentional. And if you were to override myapp.port in an external config file then fizz.buzz.foo would still end up with the value from Config.groovy, not the override from the external.

    You could make it a late-binding GString using a closure to pull the value from grails.util.Holders.config when fizz.buzz.foo is referenced rather than when it is defined:

    foo = "Port #${-> Holders.config.myapp.port}"
    

    This is different from "Port #${Holders.config.myapp.port}" which would attempt to access the config at the point where Config.groovy is being parsed.

    If the value you're defining here is one that will ultimately end up defining a property of a Spring bean (for example many of the spring-security-core plugin configuration options become bean properties) then you may be able to do

    foo = 'Port #${myapp.port}'
    

    with single rather than double quotes. This causes the resulting config entry to contain the literal string ${myapp.port}, which will be resolved against the config by the Spring property placeholder mechanism when it is used as a bean property value.

    0 讨论(0)
  • 2021-01-13 14:54

    Another way is to simply use variables within your config file like this:

    def appPort = 7500
    
    environments {
        production {
            appPort = 7600
            myapp.port = appPort
        }
    }
    
    fizz {
        buzz {
            foo = "Port #$appPort"
        }
    }
    

    And also, you don't need to send the -Dgrails.environment=development when you execute the run-app, it's the default one.

    0 讨论(0)
提交回复
热议问题