首页 > 解决方案 > 错误:现场作业需要一个找不到的“org.springframework.batch.core.Job”类型的bean

问题描述

我是 Spring Batch 的初学者,我用它开发了一个简单的项目。我收到错误消息。

Description:
Field job in com.example.demo.DemoApplication required a bean of type 
'org.springframework.batch.core.Job' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.batch.core.Job' in your 
configuration.

这是我的代码,我只有一个类:

'package com.example.demo;


import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
@EnableBatchProcessing
public class DemoApplication {



@Autowired
private JobLauncher jobLauncher;


 @Autowired
 private Job job;

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}


 @Scheduled(cron = "0 */1 * * * ?")
    public void perform() throws Exception 
    {
        JobParameters params = new JobParametersBuilder()
                .addString("JobID", String.valueOf(System.currentTimeMillis()))
                .toJobParameters();
        jobLauncher.run(job, params);
    }

 }

感谢您帮助我找到此错误的主要原因

标签: spring-bootspring-batch

解决方案


看起来您没有Job在应用程序上下文中定义 bean,或者 Spring Boot 找不到该作业。

确保定义批处理作业的配置类位于 Spring Boot 应用程序扫描的包(或子包)中(com.example.demo根据您的示例)。


推荐阅读