@Autowired是根据类型进行注入,容器中只能有一个该类型的实例;
@Resource是根据名称进行注入,容器中一种类型可以存在多个实例;
@Bean("defaultKafka")
public KafkaTemplate<Integer, String> defaultKafkaTemplate() {
KafkaTemplate<Integer, String> template = new KafkaTemplate<>(producerFactory());
template.setDefaultTopic("topic.default");
return template;
}
@Resource
private KafkaTemplate defaultKafka;
@Test
public void testDemo() throws InterruptedException {
// kafkaTemplate.send("Demo.today", "this is my two demo");
defaultKafka.sendDefault("topic.quick.default2");
//休眠2秒,为了使监听器有足够的时间监听到topic的数据
Thread.sleep(2000);
}
标签:resource,区别,autowired,KafkaTemplate,defaultKafka,topic,template,public From: https://www.cnblogs.com/ReturnOfTheKing/p/17705023.html