首页 > 解决方案 > org.springframework.beans.factory.NoSuchBeanDefinitionException:没有符合条件的 bean 类型

问题描述

2020-09-23T15:28:00.3483912Z java.lang.IllegalStateException:无法加载 ApplicationContext 2020-09-23T15:28:00.3489821Z 原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“salecChannelEventProcessor”的 bean 时出错'在文件[/home/runner/work/calculation-service/calculation-service/target/classes/com/demo/calculation/saleschannel/SalecChannelEventProcessor.class]中定义:通过构造函数参数1表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“de.demo.json.schema.JsonValidator”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:

import de.demo.json.schema.JsonValidator;
@Configuration
@ComponentScan( basePackages = {
        "com.demo",
        "de.demo" },
        excludeFilters = {
                @ComponentScan.Filter( Configuration.class )
        } )
@ImportResource("classpath:/spring-context.xml")
@Import({SwaggerConfig.class, SalesChannelSqsConfig.class})
public class SpringMvcConfig extends WebMvcConfigurationSupport {
 @Autowired private ApplicationContext applicationContext;

@Bean( name = "objectMapper" )
    public ObjectMapper getObjectMapper( JacksonService jacksonService ) {
        return jacksonService.getObjectMapper();
    }

@Bean(name = "jsonValidator")
    public JsonValidator jsonValidator() {
        return new JsonValidator();
    }
}

@Component
@Slf4j
@RequiredArgsConstructor
public class SalesChannelUpdateListerner {

  @NonNull
  private final SalesChannelService salesChannelService;

  @NonNull
  private final SalecChannelEventProcessor salecChannelEventProcessor;

  @SqsListener(value = "${sales.channel.update.queue.name}", deletionPolicy = ON_SUCCESS)
  @SneakyThrows
  public void receiveSalesChannelUpdateEvent(
      @NotificationMessage EnvelopedMessage envelopedMessage) {
    log.debug("Received message from sales channel update event queue : {}"
}


@Component
@Slf4j
@RequiredArgsConstructor
public class SalecChannelEventProcessor {

  private static final String MESSAGE_TYPE = "sales_channel_update";

  @NonNull
  private final ObjectMapper objectMapper;
  @NonNull 
  private final JsonValidator jsonValidator;



  @SneakyThrows(JsonProcessingException.class)
  public boolean isValid(EnvelopedMessage envelopedMessage) {
    if (!MESSAGE_TYPE.equals(envelopedMessage.getType())) {
      return false;
    }
    return jsonValidator.validate(envelopedMessage);
  }

标签: springspring-mvc

解决方案


您需要创建JsonValidatorbean。你需要改变你SalecChannelEventProcessor是:

@Component
@Slf4j
@RequiredArgsConstructor
public class SalecChannelEventProcessor {

  private static final String MESSAGE_TYPE = "sales_channel_update";

  @NonNull
  private final ObjectMapper objectMapper;

  @Bean
  public JsonValidator  jsonValidator(){
       return new JsonValidator();
  }

  @SneakyThrows(JsonProcessingException.class)
  public boolean isValid(EnvelopedMessage envelopedMessage) {
      if (!MESSAGE_TYPE.equals(envelopedMessage.getType())) {
          return false;
      }
      return jsonValidator().validate(envelopedMessage);
  }
}

推荐阅读