gpt4 book ai didi

java - 构造函数的参数 0 需要一个类型为 'java.lang.String' 的 bean,但找不到

转载 作者:搜寻专家 更新时间:2023-10-31 19:59:31 26 4
gpt4 key购买 nike

我正在使用 spring boot 2.X 应用程序开发 spring batch,实际上它的现有代码是我从 git 中 check out 的。运行应用程序时,由于以下错误仅对我失败,而相同的代码对其他人也有效。

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

最佳答案

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

关于java - 构造函数的参数 0 需要一个类型为 'java.lang.String' 的 bean,但找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52841620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com