Hello-
I'm using Spring dm and the javaconfig-style @Configuration annotation to move my bundle's beans configuration into a single top-level bean. This bean is explicitly declared in my bundle's /META-INF/spring/config.xml file something like this:
my configuration class was originally something like this (and worked fine if the above XML was changed to pass a <constructor-arg ... /> to the bean "myAppConfig"):Code:<beans ...> <osgi:reference id="fooService" cardinality="1..1" interface="com.example.FooService" /> <bean id="myAppConfig" class="com.example.MyAppConfig" /> <context:annotation-config /> </beans>
However, use of the constructor or setter injection starts getting burdensome when there are a lot of external OSGi services that beans depend on. So I wanted to switch to using @Autowired annotations inside my Config class, like so:Code:package com.example; @Configuration public class MyAppConfig { private FooService fooService; public MyAppConfig(FooService fooService) { this.fooService = fooService; } @Bean public SomeBean someBean() { return new SomeBean(fooService); } }
But this doesn't work, I get exceptions like:Code:package com.example; @Configuration public class MyAppConfig { @Autowired private FooService fooService; @Bean public SomeBean someBean() { return new SomeBean(fooService); } }
Basically, it looks like the imported service references specified by <osgi:reference> tags somehow don't count as beans of a particular type for purposes of @Autowired, even though they are resolved fine as the target type when used explicitly as constructor args or setter injected dependencies.Code:...BeanCreationException... Caused by: ...NoSuchBeanDefinitionException: No unique bean of type [com.example.FooService] is defined: Unsatisfied dependency of type [com.example.FooService]: expected at least 1 matching bean
Is this a bug? Am I doing something wrong? Please help, since we'd very much like this to work for field injection, since our beans depend on many OSGi services!
Thanks,
-Dave Fogel


