No qualification bean of type [java.lang.String] found for dependency when pass parameter for Bean

I am studying Spring and trying to create a bean and pass a parameter to it. My bean in the Spring configuration file looks like this:

@Bean
@Scope("prototype")
public InputFile inputFile (String path)
{
    InputFile inputFile = new InputFile();
    inputFile.setPath(path);
    return inputFile;
}

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

and in the main method I have:

InputFile inputFile = (InputFile) ctx.getBean("inputFile", "C:\\");

C:\\ is the parameter I'm trying to pass.

I run the application and get a root exception:

Called: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualification bean of type [java.lang.String] found for dependency: expected at least 1 bean that qualifies as an outsourcing candidate for this dependency. Dependency Annotations: {}

What have I done wrong and how to fix it?

+4
source share
1 answer

, 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");
+2

Source: https://habr.com/ru/post/1621591/


All Articles