title: Environment Specific Grails Service Configuration
body: <p>What is the best way to configure a Grails service with environment specific values? I believe there are two options: </p>
  
  <ol>
  <li>access grailsApplication values from within the service class or</li>
  <li>configure the service bean in a beans closure in Config.groovy or resources.groovy.</li>
  </ol>
  
  <p>I've seen a couple of posts on stackoverflow and other places that show how to do #1 (accessing grailsApplication in the service). One such post is: <a href="https://stackoverflow.com/questions/4973919/inject-grails-application-configuration-into-service">Inject grails application configuration into service</a>.</p>
  
  <p>However, I think this creates an unnecessary coupling of the service to Grails. Isn't this similar to accessing Spring's applicationContext in a pojo rather than configuring/injecting the values? Also, I've not had any luck getting this to work in a unit test of the service class as of yet. </p>
  
  <p>Two books have examples of injecting the properties (approach #2). The book The Definitive Guide to Grails 2, chapter 10, section entitled "Services in Action" shows how to do this, but without an environment specific value. The book Groovy and Grails Recipes, section 16-2 also shows an example using resources.groovy, but I've not been able to get it to work either yet. </p>
  
  <p>The following blog post also has a nice example, but not environment specific: <a href="http://ldaley.com/post/1253952347/getting-more-out-of-property-override-configuration" rel="nofollow noreferrer">http://ldaley.com/post/1253952347/getting-more-out-of-property-override-configuration</a>. Chapter 15 of the Grails Reference is consistent with these examples as well and shows how to set the properties on a bean on a per environment basis. </p>
  
  <p>However, none of the examples of either approach give any opinion or rational for doing it one way or another. Is there really no pros and cons to either approach? Wouldn't the injection approach be easier to unit test and more consistent with the spring way of doing things?</p>
  
  <p>Nathan</p>