We’ve built a web app in Grails/Groovy. In groovy, we built a pluggable caching component to provide caching of large http response streams in the Grails app. We want to inject the cache implementation at runtime based on environment, for example, when a developer is doing some local work, inject a simple Map-based cache, but in an operational environment, inject a RDBMS cache database, you get the point.
We found this reference in the Grails tutorial which uses SWITCH, and it seems to work, but it’s ugly and cumbersome. We’ve got over 5 different environments (local, dev, test, uat, and prod) and we need to inject environment-specific classes elsewhere in our code, so this approach is definitely not ideal. Are there any alternatives? Examples would be appreciated, thank you!
//from resources.groovy
beans = {
switch(Environment.current) {
case Environment.PRODUCTION:
cacheBean(com.util.OracleCacheImpl) {
//properties here
}
break
case Environment.LOCAL:
cacheBean(com.util.MockMapCache) {
//properties
}
break
}
}
You could try setting your bean definitions in
Config.groovy, using its built inenvironments {}handling, and assigning the definitions via theapplicationproperty.So in
resources.groovy:And in
Config.groovy:Thinking a little longer on it, you could probably put the whole bean definition in a
Config.groovyClosureand call it in resources.groovyresources.groovyConfig.groovy