title: Accessing Grails services from src/groovy
body: <p>Grails services are abstractions used for implementing business logic (as well as connecting to backing services/DBs, etc.) outside of a controller. So in a typical controller you might have:</p>
  
  <pre><code>class DashboardController {
      StatisticsService statsService
  
      def index() {
          // Fetches all the stats that need to be displayed to the
          // admin on the dashboard.
          AdminDashboardMetrics adm = statsService.getAdminStats()
  
          render(view: "/dashboard", model: [ adm: adm ])
      }
  }
  </code></pre>
  
  <p>Here, Grails automatically injects the <code>DashboardController</code> with a bean instance of <code>StatisticsService</code> (provided of course that the service was properly created with <code>grails create-service ...</code>).</p>
  
  <p>But what happens when I need to access <code>StatisticsService</code> <em>outside</em> of a controller, and particularly, under <strong><code>src/groovy</code></strong>?</p>
  
  <pre><code>// src/groovy/com/example/me/myapp/FizzBuzzer.groovy
  class FizzBuzzer {
      StatisticsService statsService
  
      FizzBuzzer(StatisticsService statsService) {
          super()
  
          this.statsService = statsService
      }
  
      def doSomething(MyData input) {
          MoreData result = statsService.calculate(input)
  
          // use 'result' somehow, etc....
      }
  }
  </code></pre>
  
  <p><strong>How do I properly inject <code>FizzBuzzer</code> with the same <code>StatisticsService</code> instance ad what is passed into <code>DashboardController</code>?</strong></p>