Spring是否有基于纯注释的文档?我见过的90%的文档都是为xml实现编写的。
我试图覆盖默认的JobLauncher以便能够传递JobParameters,但是我添加到下面代码中的任何JobLauncher函数都被忽略,而是运行默认的SimpleJobLauncher。
我想用4个不同的Job参数启动同一作业的4个并发实例。参数应告诉读者从哪个目录中读取。
我可以使用@Value注释传递目录的值,但这似乎只允许我设置一个在运行之间不会改变的默认值。
我在创建作业时无法配置作业(“World”被完全忽略)
@EnableBatchProcessing
@Configuration
public class BatchConfiguration{
private static final Logger log = LoggerFactory.getLogger(BatchConfiguration.class);
@Resource
public JobLauncher jobLauncher;
@Resource
public JobBuilderFactory jobBuilderFactory;
@Resource
public StepBuilderFactory stepBuilderFactory;
@Bean
@StepScope
ItemReader<Record> reader(@Value("") String directory) {
return new ExcelReader(directory);
}
@Bean
public ItemProcessor processor() {
return new ItemProcessor();
}
@Bean
public JobCompletionNotificationListener listener(){
return new JobCompletionNotificationListener();
}
@Bean
public Writer writer() {
Writer writer = new Writer();
return writer;
}
//Job Definition
@Bean
public Job job(JobCompletionNotificationListener listener) {
return jobBuilderFactory.get("job")
.listener(listener)
.flow(step1("World"))
.end()
.build();
}
@Bean
public Step step1(String directory) {
return stepBuilderFactory.get("step1")
.<Record, Record> chunk(10)
.reader(reader(directory))
.processor(processor())
.writer(writer())
.build();
}
}
我正在使用
开始我的应用程序 SpringApplication.run(Application.class, args);
有没有更好的方法来启动应用程序?
答案 0 :(得分:0)
没有关于注释文档的新闻,但我找到了传递参数的答案。由于我们没有使用xml,我们可以使用AnnotationConfigApplicationContext来指定顶层包。然后我们使用此上下文获取默认作业启动器并在启动作业时传递参数。
ApplicationContext context = new AnnotationConfigApplicationContext("package.names.*");
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("testJob");
Map<String, JobParameter> params = new HashMap<String, JobParameter>();
JobExecution execution = null;
JobParameters jp = null;
try {
params.put("directory", new JobParameter("testfolder"));
jp = new JobParameters(params);
execution = jobLauncher.run(job, jp);
} catch (Exception e) {
e.printStackTrace();
}
答案 1 :(得分:0)
如果您希望保留sb元数据(推荐),可以更新 transactionManager bean。
@Bean
public ResourcelessTransactionManager transactionManager() {
return new ResourcelessTransactionManager();
}
@Bean
public MapJobRepositoryFactoryBean mapJobRepositoryFactory(ResourcelessTransactionManager txManager)
throws Exception {
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(txManager);
factory.afterPropertiesSet();
return factory;
}
@Bean
public JobRepository jobRepository(MapJobRepositoryFactoryBean factory) throws Exception {
return factory.getObject();
}
@Bean
public JobExplorer jobExplorer(MapJobRepositoryFactoryBean factory) {
return new SimpleJobExplorer(factory.getJobInstanceDao(), factory.getJobExecutionDao(),
factory.getStepExecutionDao(), factory.getExecutionContextDao());
}
@Bean
public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
SimpleJobLauncher launcher = new SimpleJobLauncher();
launcher.setJobRepository(jobRepository);
return launcher;
}