I am new to Spring and use Spring 3.2.2. I have some beans which I injected via <constructor-arg> which works fine. Now I wanted to inject some bean via @Autowired which totally went wrong. I have done this:
beans.xml:
<context:annotation-config />
<bean id="formulaFactory" class="my.project.formula.impl.GenericFormulaFactory"
factory-method="getInstance">
<qualifier value="formulaFactory"></qualifier>
</bean>
Java source:
@Autowired
@Qualifier("formulaFactory")
private FormulaFactory formulaFactory;
(Changing the qualifier or leaving it out did not make any difference...)
And I get this error:
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"
I wonder why this error comes up. Especially the type FormulaKey irritates me. When I use the @Autowired annotation with some other bean, it works.
I have to mention that I implemented the GenericFormulaFactory as singleton via getInstance method. Maybe that causes some problems?
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.
Regards, Oliver
UPDATE: I removed the error without knowing what cause it.
What I did:
constructor-arg in xml)Now I can use the xml to configure the implementation and use it with @Autowired annotations too.
xml:
<bean id="formulaHandler" class="my.project.formula.impl.DefaultFormulaHandler">
<constructor-arg ref="formulaFactory" />
</bean>
<bean id="formulaFactory" class="my.project.formula.impl.GenericFormulaFactory" />
Still wondering why the error came up in the first place. In the implementation of the factory, a HashMap was created using FormulaKey as key, so maybe this caused trouble. If someone knows the answer, I would really like to know it.
Here is what I could gather so far:
java.lang.LinkageError comes in a situation when there are two classloaders involved in loading a class.my.project.formula.FormulaKey is loaded by one classloader and then the class loader involved in annotation processing loads it one more time.my.project.formula.FormulaKey is deferred as its no more referred while loading the context from XML .If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With