title: Java Spring - Inject a parameter into a superclass with annotations
body: <p>I have a superclass that needs the subclass to provide the implementation of the interface that the superclass runs against. </p>
  
  <p>We use Spring and DI so we can't just create the implementation with new.</p>
  
  <p>The subclass will know which bean to provide only after Spring is initialized so using a super constructor won't work. </p>
  
  <p>I also don't want to use q <code>@PostConstruct</code> setup method in the subclass as that requires the subclass developer to know how to set up the superclass. </p>
  
  <p>I want the superclass to demand the subclass to provide the reference to the implementing bean so that it can setup its member:</p>
  
  <hr>
  
  <p>The superclass uses the UrlBuilder Interface:</p>
  
  <pre><code>public class JsonConnection{
  
      private UrlBuilder urlBuilder;
  }
  </code></pre>
  
  <p>the subclass provides the implementation (<code>UrlBuilderFacebook</code>) via dependency injection.</p>
  
  <pre><code>@Component
  public class FacebookJsonConnection extends JsonConnection {
  
      @Inject
      private UrlBuilder urlBuilderFacebook;    
  }
  </code></pre>
  
  <p>the superclass can be abstract or a component, it doesn't matter. What matters is that I want to be able to create lightweight subclasses that provide the <code>UrlBuilder</code> to the superclass and <code>@Inject</code> them where needed.</p>
  
  <pre><code>@Inject
  private JsonConnection facebookJsonConnection;
  </code></pre>
  
  <p>or </p>
  
  <pre><code>@Inject
  private JsonConnection redmineJsonConnection;
  </code></pre>
  
  <p>Also I don't want the superclass to know anything about which subclass is using it.</p>