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

前端 未结 10 2431
深忆病人
深忆病人 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:11

    I was also having the same problem, for me following solution worked perfectly fine:

    I had annotated my class as @AllArgsConstructor by importing import lombok.AllArgsConstructor

    I had just removed this annotation and the code starts working.

    Hope this may help someone.

    0 讨论(0)
  • 2021-02-07 02:15

    Add a public default constructor in your class. For example.

    public User() {
    }
    
    0 讨论(0)
  • 2021-02-07 02:15

    Make sure you are using spring-boot-starter-data-jpa

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题