How do I define a bean of type 'java.lang.String' in Spring Batch?

假装没事ソ 提交于 2021-01-29 07:22:47

问题


Consider defining a bean of type 'java.lang.String' in your configuration. is the error I get with the following reader method:

@Bean
    public JdbcCursorItemReader<Assessment> getReader(DataSource datasource, String query, String name) {
        return new JdbcCursorItemReaderBuilder<Assessment>()
                .dataSource(datasource)
                .sql(query)
                .name(name)
                .rowMapper(new AssessmentMapper())
                .build();
    }

Where the step config looks like :

public Step step1(StepBuilderFactory factory,
                         DataSource dataSource,
                         ItemReader reader,
                         ExpireAssessmentWriter writer, //use custom writer
                         AssessmentItemProcessor processor,
                         PlatformTransactionManager platformTransactionManager){
        return stepBuilderFactory.get("step1")
                .transactionManager(platformTransactionManager)
                .<Assessment,Assessment>chunk(10)
                .reader(getReader(dataSource, READER_QUERY, "AssessmentReader"))
                .processor(processor)
                .writer(writer)
                .build();
    }

Why am i getting an error when trying to pass a string to getReader?

EDIT: The error comes from the second parameter in getReader. I am just trying to pass the query as a string but the Error output looks like:

Description:

Parameter 0 of method getReader in com.batch.config.ExpirationUtilityConfig 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.

回答1:


You need to remove @Bean on getReader(). Since you are calling getReader in your step definition, you don't need to declare the reader as a Spring bean.

You need to remove the ItemReader from the parameters list of your step definition as well.



来源:https://stackoverflow.com/questions/64127723/how-do-i-define-a-bean-of-type-java-lang-string-in-spring-batch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!