如何将字符串(非托管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.
感谢,阿卡什