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:
- Remove the getInstance() method and singleton nature of the factory implementation
- Added the factory interface to a handler classes constructor (and
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.