ismael
ismael

Reputation: 474

Consider defining a bean of type 'java.lang.String' in your configuration

Run spring boot application

Description:

Parameter 0 of constructor in Car required a bean of type 'java.lang.String' that could not be foun

    @Component
public class Car implements Driver {

    private String name;
    private int color;

    @Autowired
    public Car(String name, int color) {
        this.name = name;
        this.color = color;

    }

    @Override
    public void close() {

    }
}

Upvotes: 4

Views: 33941

Answers (2)

user10713028
user10713028

Reputation:

If you want to inject a Car object as component then you should provide its parameters like String name and int Color, then you might add on some configuration manager by following these steps:

  1. Remove both @Component and @Autowired annotations.

  2. Need to create a bean as shown on some configuration manager class as configuration.

    @Configuration
    public class ConfigurationManager {
    
         @Bean
         public Car car() {
            return new Car("Default", 1);
        } 
    } 
    

Upvotes: 4

Guru Prasad
Guru Prasad

Reputation: 70

I have faced a similar issue. I replaced the constructor with getters and setters to assign the values. it worked..

Upvotes: 5

Related Questions