Java Spring 期望至少有 1 个有资格作为自动装配候选者的 bean。依赖注释

问题描述 投票:0回答:1

Spring boot 应用程序在运行时抛出

No Qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate.
错误。

请在下面找到我的代码。

SpringBoot version : 2.7.9
JDK 11

public class CatalogServiceImpl extends CatalogService.CatalogServiceImplBase {

    private final DataSourceHandler dataSourceHandlerDelegate;

    private final StorefrontServiceHelper storefrontServiceHelper

    CatalogServiceImpl ( DataSourceHandler dataSourceHandlerDelegate, 
    StorefrontServiceHelper storefrontServiceHelper) {
    this.dataSourceHandlerDelegate = dataSourceHandlerDelegate;
    this.storefrontServiceHelper = storefrontServiceHelper;
 }
}

public class StorefrontServiceHelper {

    private final CatalogServiceHelper catalogServiceHelper

     public StorefrontServiceHelper(CatalogServiceHelper catalogServiceHelper){
    this.catalogServiceHelper = catalogServiceHelper;
   }

 }

public class CatalogServiceHelper {

   private final DataSourceHandler dataSourceHandlerDelegate

   public CatalogServiceHelper(DataSourceHandler dataSourceHandlerDelegate) {
    this.dataSourceHandlerDelegate = dataSourceHandlerDelegate
   }

 }

public interface DataSourceHandler {

}

public class DataSourceHandlerDelegate implements DataSourceHandler {

    public DataSourceHandlerDelegate(DataSourceHandler dynamoDBDataSourceHandler){
    this.dynamoDBDataSourceHandler = dynamoDBDataSourceHandler
   }
 }

 public class AppConfig {

 @Bean
 DataSourceHandlerDelegate 
 dataSourceHandlerDelegate(@Qualifier("DataSourceHandlerDelegate") DataSourceHandler 
 dynamoDBDataSourceHandler){
  return new DataSourceHandlerDelegate(dynamoDBDataSourceHandler)
}

低于运行时错误:

2024-10-26 16:20:31,703 [thread="main" class="o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext" cid="" id="" dd_trace_id="" dd_span_id=""] Exception encountered during context initialization cancelling refresh attempt: org.springframework.beans.factory. Unsatisfied Dependency Exception: Error creating bean with name 'catalogServiceImpl' defined in file [item-service/build/classes/java/main/com/item/grpc/CatalogServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'storefrontServiceHelper' defined in file [item-service/build/classes/java/main/com/item/service/StorefrontServiceHelper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'catalogServiceHelper' defined in file [item-service/build/classes/java/main/com/item/service/CatalogServiceHelper.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory. Unsatisfied DependencyException: Error creating bean with name 'dataSourceHandlerDelegate' defined in class path resource [com/item/config/AppConfig.class]: Unsatisfied dependency expressed through method 'dataSourceHandlerDelegate' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com..item.datasource.DataSourceHandler' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation. Qualifier (value="DataSourceHandlerDelegate")}
java spring spring-boot annotations
1个回答
0
投票

看起来 Bean 尚未创建。我之前遇到过类似的问题,我清理了项目并安装了 Maven。刷新项目并检查 .class 文件.

© www.soinside.com 2019 - 2024. All rights reserved.