Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot inject List<String> even using @Named [SPR-13585] #18162

Closed
spring-projects-issues opened this issue Oct 18, 2015 · 2 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 18, 2015

Alexandre Navarro opened SPR-13585 and commented

Impossible to inject a Generic type (like List<String>) to create a @Bean via an argument of the method even though with using @Named

A full example to describe the problem

  @Configuration
public class AppConfiguration {

   
    private static final Logger LOGGER = LoggerFactory.getLogger(AppConfiguration.class);


    @Bean
    public Map<String, String> map() {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put("" + i, "" + i);
        }
        return map;
    }
    
    
    // Does not work, inject does not work for a generic type
    // Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Named(value=map)}
    @Bean
    public List<String> list2( @Named("map") Map<String, String> map) {
        return new ArrayList<>(map.values());
    }
    
    // Work 
    @Resource(name = "map")
    private Map<String, String> map;
    
    
    // Work
    @Bean
    public List<String> list() {
        return new ArrayList<>(this.map.values());
    }
    
    
    
    /**
     * <p>main.</p>
     *
     * @param args
     */
    public static void main(String[] args) {

        final ApplicationContext context = 
        new AnnotationConfigApplicationContext(AppConfiguration.class);
        final Map<String, String> map = (Map<String, String>) context.getBean("map");
        LOGGER.info("map={}", map);
        final List<String> list = (List<String>) context.getBean("list");
        LOGGER.info("list={}", list);
//        final List<String> list2 = (List<String>) context.getBean("list2");
//        LOGGER.info("list2={}", list2);
    }


}

For me, it should work. Tell if I did something wrong or if it exists a work around just via annotation on arguments of a @Bean method (I don't want to use Wrapper class of a GenericType or @Resource the bean wanted in a field in the Configuration file like in the example).


Affects: 4.2.2

Issue Links:

Referenced from: commits 4a0fa69

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 19, 2015

Juergen Hoeller commented

I'm afraid this is by design, since @Named is just a qualifier that narrows the set of autowire candidates, not an override for the basic Collection/Map processing that @Autowired/@Inject implies.

That said, we are aware that this can be unintuitive for such scenarios and are considering to provide some solution for such scenarios in the 4.3 timeframe.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 19, 2015

Alexandre Navarro commented

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants