title: How can I access injected Grails beans in an abstract class method?
body: <p>I have an abstract class that implements much of the functionality inherited by a large number of concrete classes that are registered as beans. The beans are defined with autowiring on.  For example:</p>
  
  <pre><code>abstract class MyAbstract {
  
      MyService myService
      MyBean myBean
  
      def doSomething() {
          def value = myService.something(myBean)
      }
  }
  
  
  class MyConcrete extends MyAbstract {
      def concreteField
  
      def doSomethingElse() {
           def value = myService.somethingElse(myBean)
      }
  }
  </code></pre>
  
  <p>conf/spring/resources.groovy:</p>
  
  <pre><code>myConcrete(MyConcrete) { bean -&gt;
      bean.autowire = true
      myBean = ref(MySpecificBeanImpl)
  }
  </code></pre>
  
  <p>My Problem:</p>
  
  <p>When I run the method doSomethingElse in a MyConcrete instance, everything works as expected and the myService and myBean values are filled in with the proper values by DI. When I execute the doSomething method in a MyConcrete instance, both the myService and myBean values are null. It appears that the DI values are not visible in the abstract method inherited by the subclass. That really sucks.</p>
  
  <p>I can manually access the values using a context holder in the method or I can pass the values from the subclass to the abstract parent class using a modified method signature that accepts those values as parameters, but these are no good solutions. It completely breaks the usefulness of abstract class implementations and requires a lot of replicated code that I don't want to have to maintain.</p>
  
  <p>Even worse, in my specific case, the value of myBean is actually different for each concrete class, explicitly wired in the resources.groovy file, so the generic holder approach doesn't work.</p>
  
  <p>I've looked through a number of posts relating to this including <a href="https://stackoverflow.com/questions/19435742/grails-services-in-abstract-class">Grails services in abstract class</a> without much result in figuring out what is going on. The abstract bean definition seems to be about abstracting the bean definition properties and doesn't have anything to do with abstract classes and subclass inheritance. </p>
  
  <p>(1) Is this a limitation in the Grails/Spring DI support? 
  (2) Is there something else I need to do wrt the abstract class? </p>