我遇到了一个问题,我必须使用数组阻塞队列服务,我的体系结构类似于thi:
主计长:
@controller---
@Autowired
private AdhocService adhocService;
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String uploadFile(){
adhocService.processFile();
}服务:
我有一种方法,用它启动新线程,使用数组阻塞队列进行读写。
@service
-----
public void processFileData(InputStream inputStream, Character delimiter, Character quoteChar,String fileName,String type, String report, String desc)
{
try {
long startTime = System.nanoTime();
ExecutorService producerPool = Executors.newFixedThreadPool(1);
ExecutorService consumerPool = Executors.newFixedThreadPool(1);
producerPool.submit(new AdhocTasks(false, inputStream, delimiter, quoteChar, fileName, type, report, desc));
consumerPool.submit(new AdhocTasks(true, null, null, null, null, null, null, null));
producerPool.shutdown();
consumerPool.shutdown();我这里也有更多的方法
构成部分:
在adhoctask文件中,声明为组件:
@Component
public class AdhocTasks implements Runnable,QueueService {
@Autowired
private AdhocService adhocService;
//CONSTRUCTOR - PARAMETERIZED
public AdhocTasks(Boolean consumer, InputStream stream, Character delimiter,Character quoteChar,String fileName,String type, String report, String desc) {
this.isConsumer=consumer;
this.stream = stream;
this.delimiter=delimiter;
this.quoteChar=quoteChar;
this.fileName = fileName;
this.type = type;
this.report = report;
this.desc = desc;
}
@Override
public void run() {
if(this.isConsumer) {
consumeFileData();
}else {
readFileData();
}
}我还有consumeFileData和readFileData的实现。现在的问题是,在consumeFileData中,我调用自动配置的adhocService.consume():
但这不起作用:给我
空指针,因为adhocService对象为空
还能帮上忙吗?
发布于 2019-09-29 19:05:43
当调用该方法时,问题不会发生,这个问题在春季启动时就会发生。
AdhocTasks是一个弹簧组件。因此,它的构造函数与任何bean一样被调用。但是由于它的构造函数有参数,所以每个参数都必须被视为依赖项,所以Spring试图用spring连接它们。
第一个问题:大多数参数都找不到匹配的候选bean;第二个问题:您不想自动使用这些参数。
作为解决办法,稍微改变一下你的设计。不要让AdhocTasks成为spring,因为您不想这样做,而不是注入它所需的服务,而是在实例化它时将它作为参数传递:
@Autowired
private AdhocService adhocService;
//..
producerPool.submit(new AdhocTasks(adhocService, false, inputStream, delimiter, quoteChar, fileName, type, report, desc));https://stackoverflow.com/questions/58158234
复制相似问题