我必须创建批处理作业,这些作业需要根据作业参数创建一些配置Bean,因此我无法在应用程序的开头实例化它们。
我尝试遵循我发现的一些示例,但没有一个奏效。有人知道如何解决这个问题吗?
@SpringBootApplication
@EnableConfigurationProperties
@EnableBatchProcessing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
作业配置:
@Configuration
@Slf4j
public class JobConfiguration {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
/***
* This job will create/update the resources in AWS accordingly with the
* configuration provided.
*
* @return the job
*/
@Bean
@Qualifier("NetworkJob")
@Primary
public Job createJob() {
return jobs.get("Start Job").start(dummyTasklet()).build();
}
/***
* Create a step to verify if a service role exists or to create it if not.
*
* @return the step
*/
@Bean
@JobScope
public Step dummyTasklet() {
return steps.get("Dummy").tasklet((contribution, chunkContext) -> {
log.info("And I run ... ");
return RepeatStatus.FINISHED;
}).build();
}
}
和一些JobScope bean:
@Configuration
@Slf4j
public class GitConfiguration {
@Bean
@Qualifier("GITPath")
@JobScope
public String getGitTemporaryPath() {
return "/tmp/path";
}
@JobScope
@Bean
public Git getCustomerGitRepository(@Value("#{jobParameters[customerRepository]}") String customerRepository,
@Value("#{jobParameters[customerBranch]}") String customerBranch,
@Value("#{jobParameters[vaultSecretPath]}") String vaultSecretPath,
@Value("#{jobParameters[id]}") String id) {
return Git.cloneRepository().setURI(customerRepository)
.setDirectory(new File(getGitTemporaryPath() + id)).setBranch(customerBranch)
.setCredentialsProvider(credentialsProvider).call();
}
@Bean
@JobScope
public PropertySourcesPlaceholderConfigurer properties(Git repository) {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
val path = repository.getRepository().getWorkTree().getAbsolutePath()
+ "file.yml";
propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource(path));
return propertySourcesPlaceholderConfigurer;
}
}
答案 0 :(得分:1)
我知道了。看起来我不能在同一配置文件中包含JobScoped bean和非JobScoped bean。而且,您不应该在Job bean中使用@JobScope注释。 – Hugo Dias 19小时前