1.新建任务配置器 JobConfig.java
package com.springbatch._01helloworld;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 任务配置器
*
* @Package: com.springbatch._01helloworld
* @ClassName: jobConfig
* @author: zq
* @since: 2020/5/22 21:00
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@Configuration
public class JobConfig {
//创建 创建job的对象
@Autowired
private JobBuilderFactory jobBuilderFactory;
//创建 创建step的对象
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job helloWorldJob() {
return jobBuilderFactory.get("helloWorldJob")
.start(helloWorldStep())
.build();
}
@Bean
public Step helloWorldStep() {
return stepBuilderFactory.get("helloWorldStep")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("hello world !!!");
return RepeatStatus.FINISHED;
}
}).build();
}
}
2.新建启动器 SpringBatchConfig.java
package com.springbatch._01helloworld;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBatchDemo项目启动类
*
* @Package: PACKAGE_NAME
* @ClassName: com.springbatch._01helloworld.SpringBatchConfig
* @author: zq
* @since: 2020/5/22 20:31
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {
public static void main(String[] args) {
SpringApplication.run(SpringBatchConfig.class, args);
}
}
3.启动项目,发现helloworld被打印