下面是我的spring boot主类,其中包含@Scheduled
bean
@EnableScheduling
@EnableBatchProcessing
@SpringBootApplication(scanBasePackages = { "com.mypackage" })
public class MyMain {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public static void main(String[] args) throws Exception {
SpringApplication.run(MyMain.class, args);
}
@Scheduled(cron = "0 00 05 * * ?")
private void perform() throws Exception {
jobLauncher.run(job, new JobParameters());
}
}
我将从命令行接收参数,我需要将其作为作业参数。如何实现与@Scheduled
相同的带注释的方法不带任何参数。
发布于 2018-10-30 21:43:04
您可以注入ApplicationArguments
类型的bean,并使用它来获取应用程序参数:
@EnableScheduling
@EnableBatchProcessing
@SpringBootApplication
public class MyMain {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
@Autowired
private ApplicationArguments applicationArguments;
public static void main(String[] args) throws Exception {
SpringApplication.run(MyMain.class, args);
}
@Scheduled(cron = "0 00 05 * * ?")
private void perform() throws Exception {
String[] sourceArgs = applicationArguments.getSourceArgs();
JobParameters jobParameters; // create job parameters from sourceArgs
jobLauncher.run(job, jobParameters);
}
}
您可以在Accessing Application Arguments部分中找到有关ApplicationArguments
类型的更多详细信息。
希望这能有所帮助。
https://stackoverflow.com/questions/53064804
复制相似问题