错误:com.example.partioner.DemoApplication 中的现场作业需要找不到类型为“org.springframework.batch.core.Job”的 bean

Posted

技术标签:

【中文标题】错误:com.example.partioner.DemoApplication 中的现场作业需要找不到类型为“org.springframework.batch.core.Job”的 bean【英文标题】:Error : Field job in com.example.partioner.DemoApplication required a bean of type 'org.springframework.batch.core.Job' that could not be found 【发布时间】:2020-08-21 11:10:32 【问题描述】:

我尝试使用 spring 批处理分区数据库程序,但在尝试运行批处理时出现以下消息:

应用程序启动失败

描述:com.example.partioner.DemoApplication 中的字段作业需要一个 bean 类型 'org.springframework.batch.core.Job' 找不到。

注入点有以下注解:-@org.springframework.beans.factory.annotation.Autowired(required=true)

操作:考虑定义一个 bean 类型 'org.springframework.batch.core.Job' 在你的配置中。

这是我的主要课程:

    package com.example.partioner;

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
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.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class DemoApplication  implements CommandLineRunner 

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job job;

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

    @Override
    public void run(String... args) throws Exception 
        System.out.println("STATUS STARTED===================");

        JobParameters jobParameters = new JobParametersBuilder()
                .addString("JobId", String.valueOf(System.currentTimeMillis()))
                .addDate("date", new Date())
                .addLong("time",System.currentTimeMillis()).toJobParameters();

        JobExecution execution = jobLauncher.run(job, jobParameters);

        System.out.println("STATUS :: "+execution.getStatus());

    


这是我的 jobConfig 类:

package com.example.config;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.mysqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import com.example.mapper.CustomerRowMapper;
import com.example.model.Customer;
import com.example.partitioner.ColumnRangePartitioner;


@Configuration
public class JobConfiguration 

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    @Bean
    public ColumnRangePartitioner partitioner() 
    
        ColumnRangePartitioner columnRangePartitioner = new ColumnRangePartitioner();
        columnRangePartitioner.setColumn("id");
        columnRangePartitioner.setDataSource(dataSource);
        columnRangePartitioner.setTable("customer");
        return columnRangePartitioner;
    

    @Bean
    @StepScope
    public JdbcPagingItemReader<Customer> pagingItemReader(
            @Value("#stepExecutionContext['minValue']") Long minValue,
            @Value("#stepExecutionContext['maxValue']") Long maxValue) 
    
        System.out.println("reading " + minValue + " to " + maxValue);

        Map<String, Order> sortKeys = new HashMap<>();
        sortKeys.put("id", Order.ASCENDING);

        MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
        queryProvider.setSelectClause("id, firstName, lastName, birthdate");
        queryProvider.setFromClause("from customer");
        queryProvider.setWhereClause("where id >= " + minValue + " and id < " + maxValue);
        queryProvider.setSortKeys(sortKeys);

        JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();
        reader.setDataSource(this.dataSource);
        reader.setFetchSize(10);
        reader.setRowMapper(new CustomerRowMapper());
        reader.setQueryProvider(queryProvider);

        return reader;
    

    @Bean
    @StepScope
    public JdbcBatchItemWriter<Customer> customerItemWriter()
    
        JdbcBatchItemWriter<Customer> itemWriter = new JdbcBatchItemWriter<>();
        itemWriter.setDataSource(dataSource);
        itemWriter.setSql("INSERT INTO NEW_CUSTOMER VALUES (:id, :firstName, :lastName, :birthdate)");

        itemWriter.setItemSqlParameterSourceProvider
            (new BeanPropertyItemSqlParameterSourceProvider<>());
        itemWriter.afterPropertiesSet();

        return itemWriter;
    

    @Bean
    public Step slaveStep() 
    
        return stepBuilderFactory.get("slaveStep")
                .<Customer, Customer>chunk(10)
                .reader(pagingItemReader(null, null))
                .writer(customerItemWriter())
                .build();
    

    @Bean
    public Step step1() 
    
        return stepBuilderFactory.get("step1")
                .partitioner(slaveStep().getName(), partitioner())
                .step(slaveStep())
                .gridSize(4)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .build();
    

    @Bean
    public Job job() 
    
        return jobBuilderFactory.get("job")
                .start(step1())
                .build();
    


这是我的分区器类:

package com.example.partitioner;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;

public class ColumnRangePartitioner implements Partitioner 

    private JdbcOperations jdbcTemplate;
    private String table;
    private String column;

    public void setTable(String table) 
        this.table = table;
    

    public void setColumn(String column) 
        this.column = column;
    

    public void setDataSource(DataSource dataSource) 
        jdbcTemplate = new JdbcTemplate(dataSource);
    

    @Override
    public Map<String, ExecutionContext> partition(int gridSize) 
    
        int min = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") FROM " + table, Integer.class);

        int max = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") FROM " + table, Integer.class);

        int targetSize = (max - min) / gridSize + 1;

        Map<String, ExecutionContext> result = new HashMap<>();

        int number = 0;
        int start = min;
        int end = start + targetSize - 1;

        while (start <= max) 
        
            ExecutionContext value = new ExecutionContext();
            result.put("partition" + number, value);

            if(end >= max) 
                end = max;
            

            value.putInt("minValue", start);
            value.putInt("maxValue", end);

            start += targetSize;
            end += targetSize;

            number++;
        
        return result;
    

我不明白此消息的原因,也找不到解决方案。我想我已经放了所有必要的注释。我是初学者,希望你能帮助我。

【问题讨论】:

【参考方案1】:

DemoApplication在包com.example.partioner中,而你的工作配置类JobConfiguration在包com.example.config中。

为了让 Spring Boot 找到你的工作,你需要将你的 JobConfiguration 类移动到与你的主类 DemoApplication 相同的包或它下面的包中。

请参阅参考文档的Structuring Your Code 部分。

【讨论】:

谢谢,它的工作原理和阅读文档我知道应该避免使用“默认包”。 不客气。在这种情况下,请接受答案:***.com/help/someone-answers。请注意,接受答案与投票不同

以上是关于错误:com.example.partioner.DemoApplication 中的现场作业需要找不到类型为“org.springframework.batch.core.Job”的 bean的主要内容,如果未能解决你的问题,请参考以下文章

Pig 安装错误:错误 pig.Main:错误 2998:未处理的内部错误

Informix 错误:发生了语法错误。错误代码:-201

我收到一个错误:“MetaMask - RPC 错误:错误:错误:[ethjs-rpc] rpc 错误与有效负载”

错误精灵错误跟踪器错误

网页打开显示错误500是啥意思

PHP错误处理