错误:字段作业需要找不到类型为“org.springframework.batch.core.Job”的bean

Error : Field job in required a bean of type 'org.springframework.batch.core.Job' that could not be found

提问人:Faouzeya 提问时间:3/9/2020 最后编辑:dStulleFaouzeya 更新时间:10/19/2023 访问量:19752

问:

我是春季批次的初学者,我用它开发了一个简单的项目。我收到错误。

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-boot spring-batch

评论


答:

5赞 Mahmoud Ben Hassine 3/9/2020 #1

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

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

2赞 epaikins 5/27/2021 #2

检查您是否将 Spring 注解添加到 Job。@Service它是否是服务类, @RestController它是否是 REST API,@Repository它是否是 repo ...

2赞 nr.iras.sk 11/29/2022 #3

将注解@EnableBatchProcessing添加到 Spring 主类中

0赞 user2099069 3/23/2023 #4

我遇到了类似的问题。我在pom.xml中检查了spring-boot-starter-parent版本并使用了2.7.9版本。成功了。

评论

0赞 Horsing 3/31/2023
该问题是 3 年前发布的,其框架可能与您的不同。我猜您的建议是将 PO 的 depedencies 升级到更新版本。这可能更清楚、更有帮助。