H__D  

SpringBatch自动注入原理

  1、需要使用@EnableBatchProcessing注解

    启用批处理功能

1 @Target(ElementType.TYPE)
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 @Import(BatchConfigurationSelector.class)
5 public @interface EnableBatchProcessing {
6 
7     boolean modular() default false;
8 
9 }

  2、@EnableBatchProcessing注解导入了类 BatchConfigurationSelector.java

  3、BatchConfigurationSelector.java导入了 SimpleBatchConfiguration.class

 1 @Configuration
 2 public class SimpleBatchConfiguration extends AbstractBatchConfiguration {
 3 
 4     @Autowired
 5     private ApplicationContext context;
 6 
 7     // 任务仓库
 8     @Override
 9     @Bean
10     public JobRepository jobRepository() throws Exception {
11         return createLazyProxy(jobRepository, JobRepository.class);
12     }
13 
14     // 任务启动器
15     @Override
16     @Bean
17     public JobLauncher jobLauncher() throws Exception {
18         return createLazyProxy(jobLauncher, JobLauncher.class);
19     }
20 
21     @Override
22     @Bean
23     public JobRegistry jobRegistry() throws Exception {
24         return createLazyProxy(jobRegistry, JobRegistry.class);
25     }
26 
27     @Override
28     @Bean
29     public JobExplorer jobExplorer() {
30         return createLazyProxy(jobExplorer, JobExplorer.class);
31     }
32 
33         ......
34 }

 

  4、SimpleBatchConfiguration extends AbstractBatchConfiguration,而AbstractBatchConfiguration如下:

 1 @Configuration
 2 @Import(ScopeConfiguration.class)
 3 public abstract class AbstractBatchConfiguration implements ImportAware {
 4 
 5     @Autowired(required = false)
 6     private DataSource dataSource;
 7 
 8     private BatchConfigurer configurer;
 9     
10     // 任务工厂
11     @Bean
12     public JobBuilderFactory jobBuilders() throws Exception {
13         return new JobBuilderFactory(jobRepository());
14     }
15 
16     // 步骤工厂
17     @Bean
18     public StepBuilderFactory stepBuilders() throws Exception {
19         return new StepBuilderFactory(jobRepository(), transactionManager());
20     }
21         ......
22 }

 

  所在使用过程中,可以使用spring自动注入 任务工厂 JobBuilderFactory 步骤工厂 StepBuilderFactory

  5、关于SpringBatch任务,在SpringBoot中启动运行,是因为 BatchAutoConfiguration类 

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ JobLauncher.class, DataSource.class })
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
@ConditionalOnBean(JobLauncher.class)
@EnableConfigurationProperties(BatchProperties.class)
@Import(BatchConfigurerConfiguration.class)
public class BatchAutoConfiguration {

    // 在Spring中注入CommandLineRunner,自动运行
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "spring.batch.job", name = "enabled", havingValue = "true", matchIfMissing = true)
    public JobLauncherCommandLineRunner jobLauncherCommandLineRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
            JobRepository jobRepository, BatchProperties properties) {
        JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(jobLauncher, jobExplorer, jobRepository);
        String jobNames = properties.getJob().getNames();
        if (StringUtils.hasText(jobNames)) {
            runner.setJobNames(jobNames);
        }
        return runner;
    }
    ......
}

 

posted on 2022-01-05 23:56  H__D  阅读(424)  评论(0编辑  收藏  举报