简体   繁体   中英

Spring @Resource Expected at least 1 bean which qualifies as autowire candidate for this dependency

I'm trying to run a web app and I'm having some issues. Basically I have a controller and a process and they both share a queue.

The controller manages the files that are uploaded to the server and it puts them in the queue. In the other side, the process takes the files in the queue and uses them for other things.

I've defined the queue as a LinkedBlockingQueue and the annotation @Resource on both of them, but when I run the app, the following exception appears:

Error creating bean with name 'csvQueueConsumerBean': Injection of resource 
dependencies failed; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [java.util.concurrent.LinkedBlockingQueue] found for 
dependency: expected at least 1 bean which qualifies as autowire candidate for this 
dependency. 

The code of both clases is the following:

@RestController
@RequestMapping("/upload")
public class FileUploadControllerW {

@Resource
protected LinkedBlockingQueue<QueueObject> csvQueue;

...
}

@Component
public class CsvQueueConsumerBean{

@Resource
protected LinkedBlockingQueue<QueueObject> csvQueue;

...
}

Just for the record, both classes are not on the same package.

The reason for this is because Spring context cannot wire the Bean called

csvQueueConsumerBean

You will need to initialize its LinkedBlockingQueue dependency in the Spring config file like this:

@Bean
public LinkedBlockingQueue<QueueObject> linkedBlockingQueue(){
    LinkedBlockingQueue<QueueObject> blockingQueue = new LinkedBlockingQueue<QueueObject>();
    // do what you need here...
    return blockingQueue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM