spring-boot - 需要一个找不到的“org.springframework.kafka.core.KafkaTemplate”类型的bean
问题描述
我正在使用spring boot,apache kafka。
下面是我的控制器代码
package com.infy.controller;
@RestController
@RequestMapping(value = "/serving/")
public class ServingRequestWebControllerImpl implements ServingRequestWebController {
private static final Logger logger = LoggerFactory.getLogger(ServingRequestWebControllerImpl.class);
@Autowired
KafkaTemplate<String, TaskDetailsEntity> kafkaJsontemplate;
String TOPIC_NAME = "sample-topic";
@Override
@PostMapping(value = "/produce", consumes = { "application/json" }, produces = { "application/json" })
public String taskQueue(TaskDetailsEntity taskgDetailsEntity) {
kafkaJsontemplate.send(TOPIC_NAME, taskgDetailsEntity);
return "Serving Request Published Successfully To:- " + TOPIC_NAME;
}
}
下面是Kafka配置代码
package com.infy.config;
@Configuration
public class KafkaConfig {
@Bean
public ProducerFactory<String, String> producerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return new DefaultKafkaProducerFactory(config);
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<String, String>(producerFactory());
}
public ProducerFactory<String, ServingDetailsEntity> producerFactoryServingDetail() {
Map<String, Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return new DefaultKafkaProducerFactory(config);
}
@Bean
public KafkaTemplate<String, ServingDetailsEntity> kafkaTemplateServingDetailsListener() {
return new KafkaTemplate<String, ServingDetailsEntity>(producerFactoryServingDetail());
}
@Bean
public ConsumerFactory<String, ServingDetailsEntity> consumerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
config.put(ConsumerConfig.GROUP_ID_CONFIG, "event-group");
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return new DefaultKafkaConsumerFactory<>(config, new StringDeserializer(),
new JsonDeserializer<>(ServingDetailsEntity.class));
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, ServingDetailsEntity> kafkaTemplateTaskDetailsListener() {
ConcurrentKafkaListenerContainerFactory<String, ServingDetailsEntity> factory = new ConcurrentKafkaListenerContainerFactory<String, ServingDetailsEntity>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
以下是我的消费者代码
package com.infy.consumer;
@Service
public class KafkaConsumerService {
private static final Logger logger = LoggerFactory.getLogger(KafkaConsumerService.class);
@Autowired
private ServingService service;
@KafkaListener(topics = "sample-topic", groupId = "event-group",containerFactory = "kafkaTemplateTaskDetailsListener")
public void consumeTask(TaskDetailsEntity taskDtls){
System.out.println("Consumed Message:- \n "+taskDtls);
TaskDetailsEntity upsert = service.taskUpsert(taskDtls);
System.out.println(upsert.getId());
logger.info("\n Exit KafkaConsumerService consumeTask");
}
}
当我运行我的 Spring Boot 应用程序时,我遇到了错误
说明:com.infy.controller.ServingRequestWebControllerImpl 中的字段 kafkaJsontemplate 需要找不到类型为“org.springframework.kafka.core.KafkaTemplate”的 bean。注入点有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true) 找到以下候选但无法注入: - 'KafkaAutoConfiguration' 中的 Bean 方法'kafkaTemplate' 未加载,因为自动配置“KafkaAutoConfiguration”被排除行动:考虑重新访问上面的条目或在你的配置中定义一个“org.springframework.kafka.core.KafkaTemplate”类型的bean。
我尝试通过将以下配置添加到 application.properties,但没有运气
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
下面是我的spring boot主要代码
package com.infy;
@SpringBootApplication
public class SpringBootInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootInitializer.class, args);
}
}
请有人帮助我,我犯了什么错误。
解决方案
您想自动连接 type 的 bean KafkaTemplate<String, TaskDetailsEntity>
,但您只在 KafkaConfig 中声明了 typeKafkaTemplate<String, String>
的bean。KafkaTemplate<String, ServingDetailsEntity>
可能也是一个producerFactory()
有@Bean
注释producerFactoryServingDetail()
但没有的错误。
推荐阅读
- eclipse - 未找到模块 java.xml.bind
- node.js - Heroku Logs -TypeError:无法读取未定义的属性“替换”
- computer-science - 需要帮助理解这个证明(计算机科学)
- android - 如何从 adb shell 运行 UIAutomator2 命令?
- c - 使用 scanf() 无法将参数传递给函数
- python - 使用 for 循环将设置转换为 .csv
- reactjs - React Native 应用程序抛出“不存在捆绑 URL”但在重新加载时有效
- java - PriorityQueue 类的标准行为是什么?
- twilio - Twilio Autopilot - 字母数字字段类型问题
- python - 在 3D 空间中插入不同长度的 2D 数组