macau2008
码YouMe
2017-11-30 08:20

Spring MVC 启动完毕 不能使用依赖注入的问题

5
  • spring
  • 依赖注入

Listener如下
public class TaskInitListener implements ApplicationListener {

private static final Logger logger = Logger.getLogger(TaskInitListener.class);

private int runTime = 0;

/**
 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
 */

@Override
public void onApplicationEvent(ApplicationEvent event) {
    runTime++;
    // 第二次执行的时候Spring Servlet才初始化成功。
    if (2 == runTime) {
        TaskDataLoader.newInstance().initSystemTasks();
    }
}

}
initSystemTasks:
public class TaskManager {
private static Logger logger = Logger.getLogger(TaskManager.class);
@Autowired
private ContextLifecycleScheduledTaskRegistrar taskRegister;
@Autowired
private JobService jobService;

public void loadTasks() {
    List<JobModel> jobs = jobService.getAll();
    if (null != jobs && !jobs.isEmpty()) {
        for (JobModel job : jobs) {
            addJob(job);
        }
    }
}
}

启动时报空指针异常:

图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

8条回答