[英]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: 我已经将队列定义为LinkedBlockingQueue和它们两个的注解@Resource,但是当我运行应用程序时,出现以下异常:
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 这样做的原因是因为Spring
背景不能接线Bean
叫
csvQueueConsumerBean
You will need to initialize its LinkedBlockingQueue
dependency in the Spring
config file like this: 您将需要在Spring
配置文件中初始化其LinkedBlockingQueue
依赖关系,如下所示:
@Bean
public LinkedBlockingQueue<QueueObject> linkedBlockingQueue(){
LinkedBlockingQueue<QueueObject> blockingQueue = new LinkedBlockingQueue<QueueObject>();
// do what you need here...
return blockingQueue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.