I am getting no qualifying bean of type 'java.lang.string' available error in spring, NoSuchBeanDefinitionException in the following code. I have Test.java like the following, where I am trying to read the value from application.yaml.
@Configuration
public class Test {
@Value("${value}")
private String val;
@Bean
public Vehicle getVehicle() {
return new Vehicle(val);
}
}
In Vehicle.java
@AllArgConstructor
@Getter
@Setter
public class Vehicle {
private String model;
}
I have another class, Driver.java, from where I have to inject Vehicle object through constructor.
@Service
public class Driver {
private Vehicle v;
@Autowired
public Driver(Vehicle v) {
this.v = v;
}
application.yaml looks like this.
value: FORTUNE
price: 10000
CodePudding user response:
You can try a different way to inject this:
You don't need to create a configuration class where you inject the property as field and then create a bean of Vehicle. You can use constructor injection in Vehicle and make Vehicle a Component with @Component
annotation;
@Getter
@Setter
@Component
public class Vehicle {
private String model;
public Vehicle(@Value("${value}") String model) {
this.model = model
}
}
I personally prefer this way and I hope it works for you!