当我访问此类中的 taskService 时:
@Component
public class TaskHelper {
private final Logger logger = getLogger(this.getClass());
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
private String processInstanceId;
@EventListener
public void notify(final ContextRefreshedEvent unused) {
processInstanceId = runtimeService.startProcessInstanceByKey("Sample").getProcessInstanceId();
logger.info("started instance: {}", processInstanceId);
List<Task> task = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
logger.info("completed task: {}", task.get(0).getName());
taskService.complete(task.get(0).getId());
logger.info("completed task: {}", task);
// now jobExecutor should execute the async job
}
public String getProcessInstanceId() {
return processInstanceId;
}
它工作正常。但是我将如何访问@RestController 类中的TaskService?
我试过这样:
@RestController
public class TaskController {
@Autowired
TaskService taskService;
@RequestMapping(value = "/tasklist")
public String taskList(@RequestParam(value = "name", defaultValue = "all") String name) {
taskService.createTaskQuery().list();
return "Task: ";
}
}
但我的 taskService 始终为空。在 RestController 类中访问它的正确方法是什么?