spring - 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);
}
解决方案
您需要创建JsonValidator
bean。你需要改变你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);
}
}
推荐阅读
- java - 更改添加到堆栈的变量值而不更改堆栈?
- python - 我不断收到 TypeError:当我尝试运行我的代码时,“str”对象不能被解释为整数
- python - 尝试使用 pip 卸载时出现“PermissionError:[Errno 13]”
- python-3.x - 将文件路径(字符串)附加到列表时添加到路径的额外反斜杠
- javascript - chartjs的y轴不显示?
- c++ - 将指针传递给模板参数时类型推导的规则是什么
- sql - 从查询的列到另一个查询的行的 SQL
- python - 通过循环遍历特定数据框列中的行来生成 QR 码
- javascript - 如何摆脱js计算器中的前导零
- php - 带有嵌套 Where 子句的 Eloquent