nouniquebeandefinitionexception:没有“appproperties”类型的限定bean可用:应为单个匹配bean,但找到3个

r9f1avp5  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(246)

我正在尝试从application.properties文件加载api键值,下面是类文件。我无法启动应用程序,因为它无法找到唯一的bean。不知道我错过了什么。有人能帮忙吗。
这是我们的appproperties.java

@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = AppProperties.APP_PROPERTIES_PREFIX)
public class AppProperties {

    public static final String APP_PROPERTIES_PREFIX = "bi";

    private String accessTokenUri;
    private String clientId;
    private String clientSecret;
    private String basicAuth;
    private String apiKey;
    //getters and setters
    }

这是我们的diagnosticproperties.java

@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = "bi")
public class DiagnosticProperties extends AppProperties {
    private String diagnosisUrl;
//getters and setters 
}

这是我们的observationproperties.java

@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = "bi")
public class ObservationProperties extends AppProperties {
    private String observationUrl;
//getters and setters
}

这是我们的diagnosticserviceimpl.java

@Service
public class DiagnosticServiceImpl implements DiagnosticService {
    private static final Logger LOGGER = 
          LoggerFactory.getLogger(ObservationServiceImpl.class);

    private final WebClient webClient;
    private final DiagnosticProperties diagnosticProperties;

    public DiagnosticServiceImpl(final WebClient webClient,final DiagnosticProperties 
                  diagnosticProperties) {
        this.webClient = webClient;
        this.diagnosticProperties = diagnosticProperties;
    }

    @Override
    public Mono<DiagnosticResponse> getPatientDiagnosticDetails(final String uri) {
        return diagnosticDetails(uri);
    }
j5fpnvbx

j5fpnvbx1#

你不应该把任何注解放在 AppProperties (那可能是一个抽象类)。从它继承的类只需要 @ConfigurationProperties(prefix = "..") 以及 @Component 或者也可以使用 @EnableConfigurationProperties 从另一个配置类。
当您注入时-指定要注入的配置属性-或者像您在示例中所做的那样指定类型,或者添加 @Qualifier("bean-name") 注入点上的参数。
Spring Boot开箱即用配置 application.properties 财产来源。

相关问题