gpt4 book ai didi

spring-boot - 错误 : Field job in required a bean of type 'org.springframework.batch.core.Job' that could not be found

转载 作者:行者123 更新时间:2023-12-04 12:36:47 30 4
gpt4 key购买 nike

我是 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);
}

}

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

最佳答案

您似乎没有 Job在您的应用程序上下文中定义的 bean 或 Spring Boot 找不到作业。
确保您定义批处理作业的配置类在您的 Spring Boot 应用程序扫描的包(或子包)中(根据您的示例为 com.example.demo)。

关于spring-boot - 错误 : Field job in required a bean of type 'org.springframework.batch.core.Job' that could not be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60598680/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com