简体   繁体   English

spring 中没有“java.lang.string”类型的合格 bean 可用错误

[英]no qualifying bean of type 'java.lang.string' available error in spring

I am getting no qualifying bean of type 'java.lang.string' available error in spring, NoSuchBeanDefinitionException in the following code.我在 spring 中没有收到类型为“java.lang.string”的合格 bean 可用错误,在以下代码中出现 NoSuchBeanDefinitionException。 I have Test.java like the following, where I am trying to read the value from application.yaml.我有如下所示的 Test.java,我试图从 application.yaml 中读取值。

@Configuration
public class Test {

    @Value("${value}")
    private String val;
    
    @Bean
    public Vehicle getVehicle() {
    return new Vehicle(val);
    }
}

In Vehicle.java在 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.我有另一个类 Driver.java,我必须从其中通过构造函数注入 Vehicle 对象。

@Service
public class Driver {

private Vehicle v;

@Autowired
public Driver(Vehicle v) {
  this.v = v;
}

application.yaml looks like this. application.yaml 看起来像这样。

value: FORTUNE
price: 10000

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.您不需要创建一个配置类,在其中将属性作为字段注入,然后创建 Vehicle 的 bean。 You can use constructor injection in Vehicle and make Vehicle a Component with @Component annotation;您可以在 Vehicle 中使用构造函数注入,并使 Vehicle 成为带有@Component注释的组件;

@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!我个人更喜欢这种方式,我希望它对你有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 springboot 1.4.7没有'java.lang.String'类型的限定bean - springboot 1.4.7 No qualifying bean of type 'java.lang.String' 正常字段没有类型为 java.lang.String 的合格 bean 异常 - No qualifying bean of type java.lang.String exception for normal field 没有可用的“java.lang.String”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。 依赖注解: - No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 将参数传递给Bean时,没有找到依赖项的类型为[java.lang.String]的合格Bean - No qualifying bean of type [java.lang.String] found for dependency when pass parameter to the Bean Spring启动测试“没有可用的限定bean” - Spring boot test “No qualifying bean of type available” 没有合格的bean类型(Spring Data) - No qualifying bean of type available (Spring Data) 没有可用的合格Bean类型 - No qualifying bean of type available 类需要一个找不到的“java.lang.String”类型的 bean - Class required a bean of type 'java.lang.String' that could not be found Spring Framework没有类型为MessageSourceAccessor JAVA的合格Bean - Spring Framework No qualifying bean of type MessageSourceAccessor JAVA org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'helper'的bean时出错。 没有[java.lang.String]类型的唯一bean - org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helper' . No unique bean of type [java.lang.String]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM