【问题标题】:Load Spring integration context and access prototype bean from a Tasklet加载 Spring 集成上下文并从 Tasklet 访问原型 bean
【发布时间】: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


    【解决方案1】:

    不清楚你的整个应用程序是什么,但我会这样做:

    @Configuration
    @Import(FtpConfig.class)
    @EnableBatchProcessing
    public class JobConfig {
    

    public class FtpGetTasklet implements Tasklet {
    
         @Autowired
         BeanFactory beanFactory;
    
        ...
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
             FtpRemoteFileTemplate template =  this.beanFactory.getBean(FtpRemoteFileTemplate.class, param);
     }
    

    【讨论】:

    • 是的,我可以通过这种方式导入配置CommandLineJobRunner 导入 bean 定义,或者将集成配置放入 JobConfig
    • 为了完整起见,现阶段的应用程序只是通过创建带有运行时参数的模板,在 spring 批处理作业中使用 ftp 集成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多