1

I am facing an issue with Spring-DM and @Autowired with Osgi Services. I have defined a Spring bean + OSGI Service as following:

<bean id="my.sessionFactoryBean"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref local="my.dataSource" />
        </property>
            ...
</bean>
<osgi:service ref="my.sessionFactoryBean"
                id="my.sessionFactory" interface="org.hibernate.SessionFactory" />

I can reference, from another bundle, this service without any problem in an xml bean definiton like following:

<beans>
...
    <osgi:reference id="my.sessionFactory"
        interface="org.hibernate.SessionFactory" />
..
    <bean id="my.databaseItemReader"
        class="my.MyReader">
        <property name="sessionFactory" ref="my.sessionFactory" />
...
</beans>

My problem relies on using the @Autowired anotation like following:

public abstract class AbstractHibernateDao {

    @Autowired
    @Qualifier(value="my.sessionFactory")
    private SessionFactory sessionFactory;
    ...

I am getting the classic error:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.Sess
ionFactory my.AbstractHibernateDao.sessionFactory; nested exception is org.springfr
amework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for d
ependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {
@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qu
alifier(value=my.sessionFactory)}

If I remove the @Qualifier, I get this error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [my.sessionFactoryBean, my.sessionFactory]

Which makes me think that I do have my OSGI-Service in the Spring bean registry ... Any ideas what I am doing wrong?

FYI, I have also tried to inject OSGI-services with @ServiceReference on the setter, but Spring-DM never injects it (have some nullpointerexception)

kthackray
  • 21
  • 3

1 Answers1

0

Just a guess but when you use @Autowire, you must be doing "context:component-scan" and this in turn might be finding the additional bean that is showing up (my.sessionFactoryBean). When injected using XML, perhaps the component scan is not enabled and hence the OSGi service is properly getting resolved.

Raghuram
  • 351
  • 1
  • 11