2010-04-14 7 views

Respuesta

25

Su mejor apuesta es Config.groovy. Cualquier clase puede acceder a ConfigurationHolder.getConfig() que lo hace global, e incluso puede tener valores específicos del entorno de su variable.

someVar = "foo" 

environments { 
    production { 
     grails.serverURL = "http://www.changeme.com" 
     someOtherVar = 1000 
    } 
    development { 
     grails.serverURL = "http://localhost:8080/${appName}" 
     someOtherVar = 100 
    } 
    test { 
     grails.serverURL = "http://localhost:8080/${appName}" 
     someOtherVar = 0 
    } 
} 
+1

genial! acaba de probarlo y de hecho está funcionando. simplemente agregando la información sobre cómo obtener el valor 'someVar': String var = ConfigurationHolder.getConfig(). getProperty ('someVar'). de nuevo, muchas gracias! – firnnauriel

+8

En Groovy puede usar la importación con alias "importar org.codehaus.groovy.grails.commons.ConfigurationHolder como CH" y luego acceder a ella como "CH.config.someVar" –

+0

Intenté esto hoy y dice "ConfigurationHolder está en desuso" ¿Debo usarlo de todos modos o hay una nueva mejor manera? – Mikey

4

Con Griales 2,2

//In Config.groovy 
myVar = '/My/Root/Images/Folder' 

//In your Services/Controllers/etc.. 
import grails.util.Holders 
def grailsApplication = Holders.getGrailsApplication() 

//access you variable 
def myVar = grailsApplication.config.myVar;