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.