首页 > 解决方案 > Spring批处理作业在应用程序启动时自动启动。如何避免身份证?

问题描述

我在 Spring 中做一个简单的项目来了解 Spring 批处理是如何工作的。我的项目工作,但是当应用程序启动时执行该工作,我不希望这样。有办法避免吗?

我已经能够在需要时启动任务(例如转到由@RestController 映射的某个 URL)。唯一的问题是批处理作业在应用程序启动时启动。

但是我的 Spring 批处理配置代码是:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public FlatFileItemReader<Person> reader() {
      return new FlatFileItemReaderBuilder<Person>()
        .name("personItemReader")
        .resource(new ClassPathResource("sample-data.csv"))
        .delimited()
        .names(new String[]{"firstName", "lastName"})
        .fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
          setTargetType(Person.class);
        }})
        .build();
    }

    @Bean
    public PersonItemProcessor processor() {
      return new PersonItemProcessor();
    }

    @Bean
    public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
      return new JdbcBatchItemWriterBuilder<Person>()
        .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
        .sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
        .dataSource(dataSource)
        .build();
    }

    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
      return jobBuilderFactory.get("importUserJob")
        .incrementer(new RunIdIncrementer())
        .listener(listener)
        .start(step1)
//      .flow(step1)
//      .end()
        .build();
    }

    @Bean
    public Step step1(JdbcBatchItemWriter<Person> writer) {
      return stepBuilderFactory.get("step1")
        .<Person, Person> chunk(10)
        .reader(reader())
        .processor(processor())
        .writer(writer)
        .build();
    }
}

我有一个用于这个项目的 github 存储库: https ://github.com/Mazzotta13/SpringBatch.git

提前致谢。

标签: springspring-bootspring-batch

解决方案


在你的 application.properties 添加这个

spring.batch.job.enabled=false

希望有用


推荐阅读