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

大兔子大兔子 提交于 2020-05-25 16:58:26

问题


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 only for me and same code is working for others.

s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inputItemReader' defined in file [C:\Users\XYZ\git\main\batch\CBatchProcessing\target\classes\com\main\batchprocessing\batch\reader\InputItemReader.class]: Unsatisfied dependency expressed through **constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations**: {}


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-16 23:23:37.411 ERROR 2384 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

**Parameter 0 of constructor in com.main.batchprocessing.batch.reader.InputItemReader 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.

I have checked below

  1. All Spring components are correctly annotated with @Component, @Service, @Controller,@Repository, etc...
  2. @ComponentScan & @EnableAutoCOnfiguration is also provided.
  3. Tried giving "java.lang.String" in declarations.

Code:

    import java.util.Map;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.batch.core.ExitStatus;
    import org.springframework.batch.core.StepExecution;
    import org.springframework.batch.core.StepExecutionListener;
    import org.springframework.batch.item.file.FlatFileItemReader;
    import org.springframework.batch.item.file.mapping.JsonLineMapper;
    import 
    org.springframework.batch.item.file.separator.JsonRecordSeparatorPolicy;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.stereotype.Component;

    @Component
    public class InputItemReader extends  FlatFileItemReader<Map<String, 
     Object>> implements StepExecutionListener {

    @Autowired
    private InputFileHeaderValidator inputFileHeaderValidator; 

    @Autowired
    private FileAuditService fileAuditService;

    private final Logger log = 
    LoggerFactory.getLogger(InputItemReader.class);

    private java.lang.String inputFilePath;

    public InputItemReader(String inputFilePath) {
        setLineMapper(new JsonLineMapper());
        setRecordSeparatorPolicy(new JsonRecordSeparatorPolicy());
        setResource(new FileSystemResource(inputFilePath));
        this.inputFilePath = inputFilePath;
    }
   }

回答1:


Since you do not provide the public default constructor and you added your own non-default constructor the instantiation will fail. I would suggest you to define the input file path as property like @Value("${inputFilePath}"). If you need further initialization in your bean define a void method and annotate it with @PostConstruct and do the initialization within.




回答2:


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

public User() {
}



回答3:


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;
}



回答4:


I also had the same error:

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

Description:

Field repository in com.example.controller.CampaignController required a bean of type 'com.example.data.CustomerRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.data.CustomerRepository' in your configuration.de here

I solved this issue by adding @EnableMongoRepositories annotation in the main class:

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = CustomerRepository.class)
public class CampaignAPI {

    public static void main(String[] args) {
        SpringApplication.run(CampaignAPI.class, args);
    }
}



回答5:


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

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>



回答6:


in my case, the issue was way different.

@SpringBootApplication
@EnableNeo4jRepositories("com.digital.api.repositories") // <-- This was non-existent.
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Look at the @EnableNeo4jRepositories annotation. The package defined, was non-existent. Try defining that package, and be careful that the Repository interfaces are based there. Otherwise, Spring will not find the repositories classes that it should load up!



来源:https://stackoverflow.com/questions/52841620/parameter-0-of-constructor-in-required-a-bean-of-type-java-lang-string-that-co

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