blacelle
blacelle

Reputation: 2217

Javaconfig bean overriding failing with List injected

I consider injection of beans as a List of automatically detected beans: I introduce several beans implementing the same interface and inject all of them as a List in a later bean.

I've not been able to find official documentation related to this feature. My single source is http://www.coderanch.com/t/605509/Spring/Java-config-autowired-List

Considering this feature, I have an issue with Bean overring: I would like to override a bean defined throught a no-arg method with a bean defined with the List of detected beans. However, spring behave like the second bean definition does not exist.

It can be reproduced with the following test:

import java.util.Date;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class SpringTest {

    @Test
    public void shouldtestSpringDifferentMethodNames() {
        AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(AConfig.class, CConfig.class);
        Assert.assertEquals("overriden", ctx2.getBean("bean"));
    }

    @Configuration
    public static class AConfig {

        @Bean
        public Object bean() {
            return "not overriden";
        }

    }

    @Configuration
    public static class CConfig extends AConfig {

        @Bean
        public Date anotherBean() {
            return new Date();
        }

        @Bean
        public Object bean(List<? extends Date> someDate) {
            return "overriden";
        }

    }

}

If this is an expected behavior, how can I achieve such an overriding?

Upvotes: 1

Views: 577

Answers (2)

blacelle
blacelle

Reputation: 2217

This has been considered as a bug by the Spring team: https://jira.springsource.org/browse/SPR-10988

A recent piece of documentation can be found at: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-autowired-annotation (Thanks Alexander Kudrevatykh for the 2.5 source)

Upvotes: 0

Alexander Kudrevatykh
Alexander Kudrevatykh

Reputation: 870

  1. documentation for list autowire can be found at spring documentation

  2. overriding of beans by id or name is not official spring feature - look for that question for more details

Upvotes: 1

Related Questions