#Springrequired a bean of type 'String' that could not be found

5 messages · Page 1 of 1 (latest)

sinful wadi
#
@Component
public class Foo{

private String text;

public Foo(String txt){
this.text=txt;
}

}

Getting bean like that:

context.getBean(Foo.class)

Or

context.grtBean(Foo.class,"txt")

Or this:

 @Bean
    public MyBean myBean(String arg) {
        return new MyBean(arg);
    }

Gets me an error:
Required a bean of type 'String' that could not be found
How to create a bean with String parameter ?

clever meteorBOT
#

This post has been reserved for your question.

Hey @sinful wadi! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

placid perch
#

Since you do not provide the public default constructor and you added your own non-default constructor the instantiation will fail.
Use @Value("default value") to set a default param, or add default constructor

noble island
#

I am still fairly new to Spring, but generally when you create a bean it is for the whole application. Values are driven by configuration properties or other beans. If you want multiple beans you can but you need to give qualifiers. Your bean is expecting a string to be injected (DI like autowire) as part of the constructor. You could either hardcode the string, make a configuration class that has a bean method for each bean you want to make, or use the value approach suggested. If you need more guidance, we probably need more information on your usecase and goals.