Environment Specific Grails Service Configuration -


what best way configure grails service environment specific values? believe there 2 options:

  1. access grailsapplication values within service class or
  2. configure service bean in beans closure in config.groovy or resources.groovy.

i've seen couple of posts on stackoverflow , other places show how #1 (accessing grailsapplication in service). 1 such post is: inject grails application configuration service.

however, think creates unnecessary coupling of service grails. isn't similar accessing spring's applicationcontext in pojo rather configuring/injecting values? also, i've not had luck getting work in unit test of service class of yet.

two books have examples of injecting properties (approach #2). book definitive guide grails 2, chapter 10, section entitled "services in action" shows how this, without environment specific value. book groovy , grails recipes, section 16-2 shows example using resources.groovy, i've not been able work either yet.

the following blog post has nice example, not environment specific: http://ldaley.com/post/1253952347/getting-more-out-of-property-override-configuration. chapter 15 of grails reference consistent these examples , shows how set properties on bean on per environment basis.

however, none of examples of either approach give opinion or rational doing 1 way or another. there no pros , cons either approach? wouldn't injection approach easier unit test , more consistent spring way of doing things?

nathan

i'd use whichever you're more comfortable with. tend go accessing grailsapplication.config directly service, because lets make configuration more "semantic" (for want of better word) in sense can name configuration options after do, rather bean control. , if 2 (or more) different beans need know site administrator's email address (for example) can both read grailsapplication.config.myapp.admin.email rather having configure beans.monitorservice.destinationemail , beans.userservice.fromemail separately.

in unit tests you'd have mock grailsapplication config anyway, it's no big deal filling in test values config options services need read.


Comments