title: Injecting Service into Multiple Domain Classes for Use in Views
body: <p>My goal is to be able to reuse some code among my various domain classes to ultimately end up populating various list boxes in my gsps.  I have a service but have had little luck plugging it in. Here's my code.</p>
  
  <p>PopulateListBox Service:</p>
  
  <pre><code>import org.codehaus.groovy.grails.commons.ApplicationHolder as AH 
  import groovy.sql.Sql
  
  class PopulateListBox {
  
  def dataSource = AH.application.mainContext.dataSource
  def sql
  
  def getStates(){
  
      sql = new Sql(dataSource)
      def states = []
      sql.eachRow("SELECT stvstat_desc FROM stvstat ORDER BY stvstat_desc"){
          states.add(it.stvstat_desc)
      }
  
      sql.close()
  
      [states:states]
  }
  
  
  }
  </code></pre>
  
  <p>Inside one of my domain classes I tried using it in my constraint block like so:</p>
  
  <pre><code>def PopulateListBox
  
  static constraints = {
      nursingLicenseState(blank:false, inList:PopulateListBox.getStates())
  }
  </code></pre>
  
  <p>But this doesn't appear to work.  How do I reference my service and still be able to access it from the domain class in my view?</p>