DefaultListableBeanFactory autowired instead of own implementation after migration from Spring 3.1.2 to 4.1.6

615 views Asked by At

I have got an application which I am migrating from Spring 3.1.2 to 4.1.6. After exchanging the version several testcases fail because of an wrong autowired bean.

I want to autowire my own implementation of BeanFactory.

public class DataModel extends AbstractDataModel
{
    @Autowired
    private BeanFactory beanFactory;

    ...
}

My implementation of BeanFactory derives from ApplicationContext (which implements BeanFactory):

public class MyApplicationContext implements ApplicationContext
{
    ...
}

I define the bean in an application.xml:

<beans xmlns="http://www.springframework.org/schema/beans" ...>

    <context:annotation-config />

    <bean id="beanFactory" class="com.inxmail.xpro.server.MyApplicationContext">
    </bean>

</beans>

But now at runtime the Instance of beanFactory is of type DefaultListableBeanFactory instead of MyApplicationContext. Before the version change the instance was of type MyApplicationContext.

Please note that this kind of initialization is only for unit testing purposes of our framework. Also note that I have tried to shorten everything down and only post the relevant code snippets. Hopefully I have not forgotten an important part of the code. In that case please post what could be also relevant.

I have researched for ours in Spring documentation, migration guides and forums but was not able to not figure out what could be the problem. I assume there where some changes in the initialisation process of Spring, which causes this fault.

It would be nice if anybody could give me a hint what is going on there.

Regards Jan

1

There are 1 answers

0
Jan Mortensen On

Thanks to M. Deinum I found a solution. I marked the bean as a primary bean. So the correct bean will be autowired and my tests classes pass now.

Here is the code:

<beans xmlns="http://www.springframework.org/schema/beans" ...>

    <context:annotation-config />

    <bean id="beanFactory" class="com.inxmail.xpro.server.MyApplicationContext" primary="true">
    </bean>

</beans>

Unfortunately I found the solution incidentally and found no dokumentation of changes in the behavior of spring.