Results 1 to 3 of 3

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

  1. #1
    Join Date
    Mar 2009
    Posts
    19

    Default @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. #2
    Join Date
    Mar 2009
    Posts
    19

    Default

    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

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •