简体   繁体   中英

In the Grails configuration how do I access variables in other environments?

If I have a list environment variable in grails in one environment, how do I modify it on the cascade to other environments?

For instance, in my quartz configuration I added the following,

quartz {
    // other variables remove for clarity

    whiteListedJobs = [ Job1.class, Job2.class, Job3.class ]
}

The quartz is not specific to any single environment. But in my environment specific configurations I'd like to override this list to include more jobs for certain environments.

Something like,

environments {
    development {
        quartz {
            whiteListedJobs = whiteListedJobs + Job4.class
        }
    }
}

But unfortunately this doesn't work. My code that tries to read the quartz config variable as a list throws Cannot cast object '{}' with class 'groovy.util.ConfigObject' to class 'java.util.List' , which suggests to me that it isn't working.

What is the correct way to do this?

The problem is that the groovy ConfigSlurper doesn't provide a way to read a config entry at a different level during the parsing process. However, it is parsed as a Groovy script, so you can use local variables to avoid completely rewriting entries:

def defaultJobs = [ Job1.class, Job2.class, Job3. class ]
quartz {
    whiteListedJobs = defaultJobs
}

environments {
    development {
        quartz {
            whiteListedJobs = defaultJobs + Job4.class
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM