1

I've got a bundle (A) that exports a Hibernate transaction manager as an OSGi service, with a service interface of PlatformTransactionManager.

In another bundle (B) I define a reference to that OSGi service. Bundle B defines a bean, ReservationDao, that takes the PlatformTransactionManager as a constructor parameter. The constructor is marked with @Autowired.

When the application context loads, the following error is sometimes (often) thrown:

Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.transaction.PlatformTransactionManager]: : No matching bean of type [org.springframework.transaction.PlatformTransactionManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.transaction.PlatformTransactionManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 20 more

Basically, autowiring fails because there's no bean available that is a PlatformTransactionManager. The same error occurs for field injection.

If I create setter methods and mark those with @Autowire, the application context loads correctly.

Can anyone explain why constructor and field autowiring might fail, but setter autowiring always succeeds? Is there any way to define the OSGi references in such a way that all autowiring works? I'd hate to refactor code around a limitation of a framework.

Jeff
  • 3,669
  • 1
  • 23
  • 33

1 Answers1

0

You might need to add this to your beans.xml:

<context:annotation-config/>

Alternative you might need to use:

@Autowired
public yourconstructor(@Qualifier("platformTransactionManager")  PlatformTransactionManager ptm)
Norbert
  • 6,026
  • 3
  • 17
  • 40
  • I assume (maybe that's a problem) that if setter injection doesn't need a qualifier then injection shouldn't either. I've resigned myself to assuming that it's some bit of OSGi nonsense where OSGi service refs can exist before the exported bean has actually completed its initialization, and the autowiring process unwraps it to a nonexistent (null) bean instance. – Jeff Oct 02 '15 at 18:09