如何将字符串(非托管Bean)传递给托管Bean [英] How to pass a String (Non managed bean) to a managed bean

查看:154
本文介绍了如何将字符串(非托管Bean)传递给托管Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个春季批处理工作.有一个步骤正在调用reader方法.

I have a spring batch job. There is a step that is calling the reader method.

STEP

@Bean public Step myStep(FlatFileItemWriter<String> writer, Processor
processor,  @Value("${com.tableName}") String myTableName) {
    return stepBuilderFactory.get("step1")
        .<MyBean, String> chunk(this.chuckSize)
        .reader(reader(myTableName, this.myRowMapper))
        .processor(processor)
        .writer(writer)
        .build(); 
}

阅读器 工作

@Bean
public <T> JdbcCursorItemReader<T> reader(@Value("${com.tableName}") String tableName, RowMapper<T> rowMapper) {
    JdbcCursorItemReader<T> jdbcCursorItemReader = new JdbcCursorItemReader<>();
    String query = "select * from " + tableName;
    jdbcCursorItemReader.setDataSource(dataSource);
    jdbcCursorItemReader.setSql(query);
    jdbcCursorItemReader.setRowMapper(rowMapper);

    return jdbcCursorItemReader;
}

我希望我的读者使用动态tableName.因此,我将读者更改为如下所述.

I want my reader to take dynamic tableNames. So I changed the reader to as mentioned below.

@Bean
public <T> JdbcCursorItemReader<T> reader(String tableName, RowMapper<T> rowMapper) {
    JdbcCursorItemReader<T> jdbcCursorItemReader = new JdbcCursorItemReader<>();
    String query = "select * from " + tableName;
    jdbcCursorItemReader.setDataSource(dataSource);
    jdbcCursorItemReader.setSql(query);
    jdbcCursorItemReader.setRowMapper(rowMapper);

    return jdbcCursorItemReader;
}

这将导致以下错误.

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method reader in com.walgreens.oracleextractionbatch.OracleExtractionJobConfiguration required a bean of type 'java.lang.String' that could not be found.


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

在激烈的谷歌搜索后,我尝试了许多变通方法.但是我想我缺少一些基本的东西.请帮忙. TIA

I tried so many workarounds after intense googling. But I think I am missing something basic. Please help. TIA

推荐答案

我更改了 Reader Writer Step 的范围豆原型.我将处理器bean的范围保留为(Singleton),因为所有情况下的处理器逻辑都是相同的.

I changed the scope of the Reader, Writer and the Step bean to prototype. I left the scope of processor bean as is(Singleton) because the processor logic is the same for all the scenarios.

感谢,阿卡什

这篇关于如何将字符串(非托管Bean)传递给托管Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆