Parameter 0 of constructor in required a bean of type 'java.lang.String' that could not be found

前端 未结 10 2457
深忆病人
深忆病人 2021-02-07 01:41

I am working on spring batch with spring boot 2.X application, actually its existing code i am checked out from git. While running the application it fails due to below error on

10条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 02:18

    You defined something like this:

    @Component
    public class InputItemReader{
    
       public InputItemReader(String input){
         ...
       }
    }
    

    The name of your class suggest that your object is not a bean, just a simple object. You should try to use it in classic way:

    new InputItemReader(myString);
    

    or to have a static method to process the input String.

    Explanation: Spring IoC container will try to instantiate a new InputItemReader object like this :

    new InputItemReader( -- WHAT TO PUT HERE? --) 
    

    and will fail to call your constructor, because it will not know what you do actually expect and input string.

    UPDATE: Your problem can be solved by removing @Component annotation and defining the bean in a configuration like this:

    @Bean
    public InputItemReader inputItemReader(InputFileHeaderValidator inputFileHeaderValidator, FileAuditService fileAuditService){
        InputItemReader inputItemReader = new InputItemReader("--HERE SHOULD BE ACTUAL PATH---");
        // set the required service, a cleaner approach would be to send them via constructor
        inputItemReader.setFilteAuditService(fileAuditService);
        inputItemReader.setInputFileHeaderValidator(inputFileHeaderValidator);
        return inputItemReader;
    }
    

提交回复
热议问题