构造函数中的参数 0 需要一个无法找到的“java.lang.String"类型的 bean [英] Parameter 0 of constructor in required a bean of type 'java.lang.String' that could not be found

查看:113
本文介绍了构造函数中的参数 0 需要一个无法找到的“java.lang.String"类型的 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring boot 2.X 应用程序处理 spring batch,实际上它的现有代码是我从 git 中检出的.运行应用程序时,由于以下错误而失败,仅适用于我,其他代码也适用.

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.

我在下面检查过

  1. 所有 Spring 组件都使用@Component、@Service、@Controller、@Repository 等正确注释...
  2. @ComponentScan &还提供了 @EnableAutoCONfiguration.
  3. 尝试在声明中给出java.lang.String".

代码:

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

推荐答案

由于您没有提供公共默认构造函数并且您添加了自己的非默认构造函数,因此实例化将失败.我建议您将输入文件路径定义为 @Value("${inputFilePath}") 之类的属性.如果你需要在你的 bean 中进一步初始化,定义一个 void 方法并用 @PostConstruct 注释它并在其中进行初始化.

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.

这篇关于构造函数中的参数 0 需要一个无法找到的“java.lang.String"类型的 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆