com.example.demo.config.BatchLauncher 中的欄位 jobLauncher 需要找不到型別為“org.springframework.batch.core.launch.JobLauncher”的 bean。
注入點有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)
行動:
考慮在你的配置中定義一個 'org.springframework.batch.core.launch.JobLauncher' 型別的 bean。
@Component
public class BatchLauncher {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public BatchStatus run() throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
JobRestartException, JobInstanceAlreadyCompleteException {
JobParameters parameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, parameters);
return jobExecution.getStatus();
}
}
uj5u.com熱心網友回復:
你有很多方法來解決它,所有這些都是解決在 Spring IOC 容器中檢測到JobLuncher,因為例外說 IOC 無法找到JobLuncher類注入BatchLuncher類的 bean ,簡單使用@Autowired是不夠的實際上,您實際上是在告訴 IOC將提供JobLuncher類的實體,但是在編譯時他找不到要注入的實體,因此您需要提供它的實體。
- 將
@Service
or@Component
注釋添加到 JobLuncher 的實作中。 - 在@Config注解的某個自定義類中注入JobLuncher類的實體,并通過@Bean注解JobLuncher的方法注入器以供IOC掃描。
@Configuration
public class JobLuncherConfig {
@Bean
public JobLuncher jobLuncher() {
//return an implementation or concrete class of JobLuncher class-interface
return new JobLuncher(...);
}
}
確實還有很多其他方法,我建議更深入地了解 Spring 容器和控制反轉,您可以從這里開始。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/437301.html
下一篇:JPQLSpring的全域過濾器