SpringBoot & SpringBatch如何启动及退出

本文解析了如何仅用一行代码在SpringBoot中启动SpringBatch应用,详细介绍了@SpringBootApplication注解的功能,包括自动配置和组件扫描的过程。同时,探讨了SpringApplication如何调用CommandLineRunner执行批处理作业,并解释了应用如何通过状态码优雅地退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

启动篇

在使用SpringBoot时,只需要一行代码就可以启动配置好的SpringBatch应用。

@SpringBootApplication
public class MainClass {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainClass.class);
        int exitCode = SpringApplication.exit(context );
        System.exit(exitCode);
    }
}

好奇是如何做到的。

首先SpringBootApplication注解有如下配置:

1.SpringBootConfiguration 中引用了Configuration注解。

2.EnableAutoConfiguration

    (1)从环境中读取spring.boot.enableautoconfiguration属性,若为true则开启自动配置。

    (2)用beanClassLoader 读取 spring-autoconfigure-metadata.properties

    (3) 通过springfactories的方式获取到候选配置,去重,去exclutions

    (4) List<String> filter(List<String> configurations, AutoConfigurationMetadata autoConfigurationMetadata)方法过滤不需要的配置 

    (5)派发AutoConfigurationImportEvent

    (6)返回Import列表。

3.ComponentScan

详细内容https://blog.youkuaiyun.com/qq_26000415/article/details/78947283这篇文章说的比较清楚。可以参考。

 

--------------------

第二部分是SpringApplication中,会调用callRunner方法如下:

	private void callRunners(ApplicationContext context, ApplicationArguments args) {
		List<Object> runners = new ArrayList<>();
		runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
		runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
		AnnotationAwareOrderComparator.sort(runners);
		for (Object runner : new LinkedHashSet<>(runners)) {
			if (runner instanceof ApplicationRunner) {
				callRunner((ApplicationRunner) runner, args);
			}
			if (runner instanceof CommandLineRunner) {
				callRunner((CommandLineRunner) runner, args);
			}
		}
	}

这里会获取到SpringBatch 自动配置, BatchAutoConfiguration中注册的CommandLineRunner bean。进而执行。

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = "spring.batch.job", name = "enabled", havingValue = "true", matchIfMissing = true)
	public JobLauncherCommandLineRunner jobLauncherCommandLineRunner(
			JobLauncher jobLauncher, JobExplorer jobExplorer) {
		JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(
				jobLauncher, jobExplorer);
		String jobNames = this.properties.getJob().getNames();
		if (StringUtils.hasText(jobNames)) {
			runner.setJobNames(jobNames);
		}
		return runner;
	}

可以通过覆盖这个Bean来自定义CommandLineRunner,以此在客户无感知的情况下嵌入自定义行为。

 

 

退出篇

参考JobLauncherCommandLineRunner 可知,在execute方法中发布了gApplicationEvent

	protected void execute(Job job, JobParameters jobParameters)
			throws JobExecutionAlreadyRunningException, JobRestartException,
			JobInstanceAlreadyCompleteException, JobParametersInvalidException,
			JobParametersNotFoundException {
		JobParameters nextParameters = new JobParametersBuilder(jobParameters,
				this.jobExplorer).getNextJobParameters(job).toJobParameters();
		JobExecution execution = this.jobLauncher.run(job, nextParameters);
		if (this.publisher != null) {
			this.publisher.publishEvent(new JobExecutionEvent(execution));
		}
	}

JobExecutionEvent 事件由JobExecutionExitCodeGenerator监听。并以此生成退出码。故可使用开篇代码带状态码地退出应用,以便容器等运行环境感知到异常退出:

@SpringBootApplication
public class MainClass {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainClass.class);
        int exitCode = SpringApplication.exit(context );
        System.exit(exitCode);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值