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

k97glaaz  于 2022-12-04  发布在  Spring
关注(0)|答案(3)|浏览(163)

我是一个初学者与 Spring 批处理,我开发了一个简单的项目与它。我得到的错误。

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

 }

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

qgzx9mmu

qgzx9mmu1#

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

h7wcgrx3

h7wcgrx32#

检查是否已将Spring注解添加到作业。如果是服务类,则为@Service;如果是REST API,则为@RestController;如果是存储库,则为@Repository...

ykejflvf

ykejflvf3#

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

相关问题