title: @Specializes in Spring
body: <p>CDI has the feature of <a href="http://docs.jboss.org/weld/reference/2.2.0.Final/en-US/html/specialization.html" rel="nofollow">Specialization</a>, and I'm looking for that in the Spring world.</p>
  
  <p><strong>Details.</strong>
  In CDI, the <a href="http://docs.jboss.org/cdi/api/1.1/javax/enterprise/inject/Specializes.html" rel="nofollow"><code>@Specializes</code></a> annotation allows one to change the behaviour of a bean just by overriding it. This is completely transparent to users of that bean, e.g. if we'd have</p>
  
  <pre><code>public class OneBean {
    public String whoAmI() { return "OneBean"; }
  }
  
  @Specializes
  public class AnotherBean extends OneBean {
    @Override
    public String whoAmI() { return "AnotherBean"; }
  }
  </code></pre>
  
  <p>we could</p>
  
  <pre><code>public class SomewhereElse {
    @Inject
    OneBean oneBean; // we know nothing of AnotherBean here!
  
    public void guessWhosThere() {
      return oneBean.whoAmI(); // yet it returns "AnotherBean"
    }
  }
  </code></pre>
  
  <p>This gets really useful as soon as <code>OneBean</code> is actually used with and without <code>AnotherBean</code>. For example, if <code>OneBean</code> is in <code>one.jar</code> and <code>AnotherBean</code> is in <code>another.jar</code>, we can change the bean's behaviour just by reconfiguring the classpath.</p>
  
  <p><strong>Question.</strong> Does something like Specialization also exist in Spring?</p>
  
  <p>I could only find the <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Primary.html" rel="nofollow"><code>@Primary</code></a> annotation, which however has a different semantics: <code>@Primary</code> does not replace one bean, but only marks one of multiple alternatives as the <em>primary</em> one. Especially, as I understood, I could not build a deep inheritance hierarchy as it's possible with <code>@Specializes</code>.</p>