当前位置: 首页 > 知识库问答 >
问题:

当将参数传递给bean时,没有找到[java.lang.String]类型的合格bean

范承志
2023-03-14
@Bean
@Scope("prototype")
public InputFile inputFile (String path)
{
    InputFile inputFile = new InputFile();
    inputFile.setPath(path);
    return inputFile;
}
public class InputFile {
    String path = null;
    public InputFile(String path) {
        this.path = path;
    }
    public InputFile() {

    }
    public String getPath() {
       return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
}
InputFile inputFile = (InputFile) ctx.getBean("inputFile", "C:\\");

由:org.springframework.beans.factory.nosuchbeanDefinitionException引起:没有找到用于依赖项的[java.lang.string]类型的合格bean:预期至少有1个bean可以作为此依赖项的autowire候选bean。依赖项批注:{}

我做错了什么,怎么修复?

共有1个答案

终翰学
2023-03-14

您需要向参数传递一个值,然后只有您才能访问bean。这就是异常中给出的消息。

在方法声明上方使用@value注释,并向其传递一个值。

@Bean
@Scope("prototype")
@Value("\\path\\to\\the\\input\\file")
public InputFile inputFile (String path)
{
    InputFile inputFile = new InputFile();
    inputFile.setPath(path);
    return inputFile;
}

此外,在访问这个bean时,您需要使用下面的代码来访问它

InputFile inputFile = (InputFile) ctx.getBean("inputFile");
 类似资料:
  • 我通过使用Spring和Hibernate创建实体、服务和服务的JUnit测试开始了我的项目。所有这些都很管用。然后,我添加了spring-mvc来使用许多不同的分步教程来制作这个web应用程序,但是当我试图使用注释制作Controller时,我在部署期间从Glassfish得到错误。我想由于某种原因Spring没有看到我的服务,但经过多次尝试,我仍然无法处理它。 /webapp/web-inf/

  • 当试图用包含所有上下文配置的抽象类运行stepdefs时,spring看到2个不同的beans parent和step def 我使用的是Spring Booking版本:2.6.4,JUnit 5和Cucumber版本7.2.3 异常堆栈跟踪: io.cucumber.core.runtime.CucumberExecutionContext.runTestCase:没有可用的“Cucumber

  • 我尝试自动连接我的mapstruct mapper: 这是可行的: 但是为什么我不能使用: 我得到以下错误: 导致原因:org . spring framework . beans . factory . nosuchbeandidefinitionexception:没有类型为“pl . comp . window . application . mapper . windowdtomapper

  • 我正在使用Camel 2.13.1我想将一个类作为参数传递给bean中的一个方法 我能做些像 我知道我可以在header中发送限定类名,并在bean中使用它,但感觉不太对。还有其他选择吗? 我看到了这个链接,但它不适合我Apache Camel-Spring DSL-将字符串参数传递给bean方法

  • 问题内容: 我正在尝试构建一个全新的Spring Framework 4.0项目,而没有所有神奇的东西,而只是简单地将它踢过去。 我在这里关注该教程:http : //spring.io/guides/tutorials/data/并取得了一些成功。我只是停留在这一点上。 当我运行此单元测试时,得到以下堆栈跟踪: 根据观察和研究,似乎是在告诉我有两个EntityManager类。第一个来自hibe

  • 我很难理解为什么会发生这种错误。我正在将教程移植到最新版本的Spring、Hibernate和WildFly。我从命令行运行,使用Maven构建和测试应用程序。我得到以下错误: 2015年7月10日下午2:18:03 org.springframework.test.context。TestContextManager prepareTestInstance SEVERE:允许TestExecut