Announcement Announcement Module
Collapse

Spring Dynamic Modules forum decommissioned in favor of Eclipse Gemini Blueprint

With the official first release of Eclipse Gemini Blueprint shipped, the migration of the Spring Dynamic Modules code base to the Eclipse Foundation, as part of the Gemini project, has been completed.

As such, this forum has been decommissioned in favour of the Eclipse Gemini forums.
See more
See less
@Autowired inside @Configuration doesn't seem to work for imported Services Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired inside @Configuration doesn't seem to work for imported Services

    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:

    Code:
    <beans ...>
    
    	<osgi:reference id="fooService" cardinality="1..1" interface="com.example.FooService" />
    	
    	<bean id="myAppConfig" class="com.example.MyAppConfig" />
    	
    	<context:annotation-config />
    
    </beans>
    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:
    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);
        }
    
    }
    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 {
    
        @Autowired
        private FooService fooService;
    
        @Bean
        public SomeBean someBean() {
    
            return new SomeBean(fooService);
        }
    
    }
    But this doesn't work, I get exceptions like:

    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
    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.

    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

  • #2
    Any spring folks care to weigh in on this? We're trying hard to figure out how all these new features fit together in the Spring 3.0/ Spring DM world, and we keep running into what seems to be unexplored corners of broken or unexplained combinations of features...

    Thanks,
    - Dave Fogel

    Comment


    • #3
      Hi Dave.

      You've hit an annoying problem caused by proxying and annotations. Due to the way things are implemented in Java, the annotations on the user class are not available on the proxy class (as they are not inherited).
      That's why as far as it can tell, the osgi:reference class doesn't contain the annotations declared on the parent. Neither JDK proxies nor CGLIB allow annotations to be copied inside the proxy currently.

      Comment

      Working...
      X