【发布时间】:2023-03-23 02:02:02
【问题描述】:
给定以下spring集成配置
...
import org.springframework.integration.ftp.session.FtpRemoteFileTemplate;
@Configuration
public class FtpConfig {
@Bean
@Scope("prototype")
public FtpRemoteFileTemplate template(String param) {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
// these parameters can be determined at runtime, that's why I need param
sf.setHost("some host");
sf.setUsername("some user");
sf.setPassword("some pass");
sf.setPort(21);
return new FtpRemoteFileTemplate(sf);
}
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FtpConfig.class);
FtpRemoteFileTemplate template = ctx.getBean(FtpRemoteFileTemplate.class, "somevalue");
String port = template.getSession().getHostPort();
System.out.println(port);
ctx.close();
}
}
我可以引导应用程序上下文并按类型获取FtpRemoteFileTemplate bean,并将运行时参数传递给它"somevalue"。
我希望能够在使用 CommandLineJobRunner 实用程序运行的 Spring Batch 作业中加载此配置:
java org.springframework.batch.core.launch.support.CommandLineJobRunner mypackage.JobConfig jobname param=somevalue
这是我的工作配置:
@Configuration
@EnableBatchProcessing
public class JobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job jobname() {
return this.jobBuilderFactory.get("jobname")
.start(downloadCsvFile())
.build();
}
@Bean
public Step downloadCsvFile() {
return this.stepBuilderFactory.get("downLoadFile")
.tasklet(ftpGetTasklet())
.build();
}
@Bean
public FtpGetTasklet ftpGetTasklet() {
return new FtpGetTasklet();
}
}
我的问题是如何从FtpGetTasklet 加载和访问FtpRemoteFileTemplate bean?
public class FtpGetTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
String param = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("param");
System.out.println("FtpGetTasklet execute: " + param);
// how to load and access the template as I do in FtpConfig's main method???
//
// AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FtpConfig.class);
// FtpRemoteFileTemplate template = ctx.getBean(FtpRemoteFileTemplate.class, param);
return RepeatStatus.FINISHED;
}
}
【问题讨论】:
标签: spring-batch spring-integration