Skip to content

Cannot @Autowired java.util.concurrent.BlockingQueue [SPR-7798] #12454

Closed

Description

@spring-projects-issues

Marty Kube opened SPR-7798 and commented

Cannot @Autowire to inject an instance of java.util.concurrent.BlockingQueue. The above forum has a post from 2009. I was able to reproduce this problem and have attached a minimal example. This works with XML based bean configuration but not with the annotation @Autowired.

AppComponent.java:

 
package com.example;

import java.util.concurrent.BlockingQueue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class AppComponent {

	@Autowired
	public BlockingQueue queue;

	public static void main(String[] args) {

		new ClassPathXmlApplicationContext("com/example/context.xml").getBean(AppComponent.class);
	}
}

Config.java:

 
package com.example;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {

	@Bean
	public BlockingQueue blockingQueue() {
		return new LinkedBlockingQueue();
	}
}

context.xml:

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
	<context:component-scan base-package="com.example" />
</beans>

The runtime error is:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appComponent': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public java.util.concurrent.BlockingQueue com.example.AppComponent.queue; nested exception is org.springframework.beans.FatalBeanException: No element type declared for collection [java.util.concurrent.BlockingQueue]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.example.AppComponent.main(AppComponent.java:17)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public java.util.concurrent.BlockingQueue com.example.AppComponent.queue; nested exception is org.springframework.beans.FatalBeanException: No element type declared for collection [java.util.concurrent.BlockingQueue]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
	... 13 more
Caused by: org.springframework.beans.FatalBeanException: No element type declared for collection [java.util.concurrent.BlockingQueue]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:740)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
	... 15 more

Affects: 3.0.5

Reference URL: http://forum.springsource.org/showthread.php?t=72474

Attachments:

Issue Links:

Activity

  1. spring-projects-issues commented on Jul 5, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Michael F commented

    (I posted the same response in the forum thread linked above.)

    If you want auto-wire the Queue try to use JSR-250's @Resource annotation and a reasonable qualifier. Spring supports these annotations and should correctly inject the queue.)

    @Component
    public class B {
      @Resource(name="myBlockingQueue")
      private BlockingQueue<Integer> blockingQueue;
    }
    

    @Autowired and @Resource behave differently.

    Background info can be found in the documentation: Fine-tuning annotation-based autowiring with qualifiers

  2. spring-projects-issues commented on Jul 7, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Please see #13164, which aims to address this and a number of other similar issues related to collection autowiring.

  3. spring-projects-issues commented on Jul 10, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Marty Kube commented

    Great, thanks. @Resource solved the issue.

  4. added
    type: bug
    in: core
    status: duplicate
    and removed
    type: bug
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions