title: Spring java.lang.LinkageError: loader constraint violation: loader previously initiated loading for a different type with name X
body: <p>I am new to Spring and use Spring 3.2.2. I have some beans which I injected via <code>&lt;constructor-arg&gt;</code> which works fine. Now I wanted to inject some bean via <code>@Autowired</code> which totally went wrong. I have done this:</p>
  
  <p>beans.xml:</p>
  
  <pre><code>&lt;context:annotation-config /&gt;
  &lt;bean id="formulaFactory" class="my.project.formula.impl.GenericFormulaFactory"
      factory-method="getInstance"&gt;
  &lt;qualifier value="formulaFactory"&gt;&lt;/qualifier&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Java source:</p>
  
  <pre><code>@Autowired
  @Qualifier("formulaFactory")
  private FormulaFactory formulaFactory;
  </code></pre>
  
  <p>(Changing the qualifier or leaving it out did not make any difference...)</p>
  
  <p>And I get this error:</p>
  
  <p><code>java.lang.LinkageError: loader constraint violation: loader (instance of org/springframework/context/support/ContextTypeMatchClassLoader$ContextOverridingClassLoader) previously initiated loading for a different type with name "my/project/formula/FormulaKey"</code></p>
  
  <p>I wonder why this error comes up. Especially the type <code>FormulaKey</code> irritates me. When I use the <code>@Autowired</code> annotation with some other bean, it works.</p>
  
  <p>I have to mention that I implemented the GenericFormulaFactory as singleton via <code>getInstance</code> method. Maybe that causes some problems?</p>
  
  <p>The application is a stand-alone app. I checked all the jars for duplicity too and I do not assume that this is the cause of the problem because the error relates to my own classes.</p>
  
  <p>Regards,
  Oliver</p>
  
  <p><strong>UPDATE:</strong>
  I removed the error without knowing what cause it.</p>
  
  <p>What I did:</p>
  
  <ol>
  <li>Remove the getInstance() method and singleton nature of the factory implementation</li>
  <li>Added the factory interface to a handler classes constructor (and <code>constructor-arg</code> in xml)</li>
  </ol>
  
  <p>Now I can use the xml to configure the implementation and use it with <code>@Autowired</code> annotations too.</p>
  
  <p>xml:</p>
  
  <pre><code>&lt;bean id="formulaHandler" class="my.project.formula.impl.DefaultFormulaHandler"&gt;
      &lt;constructor-arg ref="formulaFactory" /&gt;
  &lt;/bean&gt;
  &lt;bean id="formulaFactory" class="my.project.formula.impl.GenericFormulaFactory" /&gt;
  </code></pre>
  
  <p>Still wondering why the error came up in the first place. In the implementation of the factory, a <code>HashMap</code> was created using <code>FormulaKey</code> as key, so maybe this caused trouble. If someone knows the answer, I would really like to know it.</p>