Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot/SpringKafka应用程序中的Autowired KafkaTemplate抛出空指针_Java_Spring_Spring Boot_Spring Kafka - Fatal编程技术网

Java SpringBoot/SpringKafka应用程序中的Autowired KafkaTemplate抛出空指针

Java SpringBoot/SpringKafka应用程序中的Autowired KafkaTemplate抛出空指针,java,spring,spring-boot,spring-kafka,Java,Spring,Spring Boot,Spring Kafka,我试图在Spring引导应用程序中使用KafkaTemplate将消息写入Kafka主题 我创建了一个Kafkanconfig类: @Configuration @EnableKafka public class KafkaConfig { @Value("${kafka.broker.address}") private String brokerAddress; @Bean public ProducerFactory<Integ

我试图在Spring引导应用程序中使用KafkaTemplate将消息写入Kafka主题

我创建了一个Kafkanconfig类:

@Configuration
@EnableKafka
public class KafkaConfig {

    @Value("${kafka.broker.address}")
    private String brokerAddress;

    @Bean
    public ProducerFactory<Integer, String> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerAddress);
        props.put(ProducerConfig.RETRIES_CONFIG, 0);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
        props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        return props;
    }

    @Bean
    public KafkaTemplate<Integer, String> kafkaTemplate() {
        return new KafkaTemplate<Integer, String>(producerFactory());
    }

}

卡夫卡·斯普林(kafka spring)在过去使用几乎相同的代码对我来说工作得很好,所以我有点困惑为什么这次自动布线不起作用。你能看出我做错了什么吗?

你用豆子太快了

postProcessBeforeInitialization
…在设置属性之前调用

编辑

实施
SmartLifecycle
并移动

watchService.monitor(path);
start()

EDIT2

也就是说;这很奇怪

    WatchService watchService = new WatchService();
    watchService.monitor(path);

您正在用post-construct方法创建一个新实例-当然它不会自动连接,因为它不是由Spring管理的。

您使用bean的时间太快了

postProcessBeforeInitialization
…在设置属性之前调用

编辑

实施
SmartLifecycle
并移动

watchService.monitor(path);
start()

EDIT2

也就是说;这很奇怪

    WatchService watchService = new WatchService();
    watchService.monitor(path);

您正在使用后期构造方法创建一个新实例-当然不会自动连接,因为它不是由Spring管理的。

请参阅我的新编辑-您的
WatchService
实例不是由Spring管理的。非常感谢,@GaryRussell。关于
WatchService
不由Spring管理,您是对的。我将
@Postconstruct
移动到
monitor
方法,并删除了
watchPath
方法(其中包含
newwatchservice()
。我现在确实有同样的问题,但我没有明白你的意思,这是我的服务类的外观,配置类与上面的“@service public class ReportingInventoryError{@Value(${kafka.producer.inventorycountupdate.errortopic}”)私有字符串topicName;@Autowired private KafkaTemplate;public void start(){ReportingTask task=new ReportingTask();ReportingTask.TaskDetails details=new ReportingTask.TaskDetails();template.send(topicName,“messageKey”,task.setTaskDetails(details));}.@GaryRussell不要在注释中添加代码。这很难阅读。也不要在对2年前的答案的注释中提出新问题。改为提出新问题。@GaryRussell,我对这个问题也有类似的问题。你能看看我的新编辑吗?你的
WatchService
实例不是Spring管理的。非常感谢,@GaryRussell。你是谁e关于
WatchService
不是由Spring管理的说法是正确的。我将
@Postconstruct
移动到
monitor
方法,并删除了
watchPath
方法(其中包含
新的WatchService()
。我现在确实有同样的问题,但我没有明白你的意思,这是我的服务类的外观,配置类与上面的“@service public class ReportingInventoryError{@Value(${kafka.producer.inventorycountupdate.errortopic}”)私有字符串topicName;@Autowired private KafkaTemplate;public void start(){ReportingTask task=new ReportingTask();ReportingTask.TaskDetails details=new ReportingTask.TaskDetails();template.send(topicName,“messageKey”,task.setTaskDetails(details));}.@GaryRussell不要在注释中添加代码。这很难阅读。也不要在对2年前的答案的注释中提出新问题。改为提出新问题。@GaryRussell,我对这个问题也有类似的问题。你能检查一下吗?这个问题不可重现。GitHub repo已关闭。这个问题不可重现。GitHub repo正在运行wn。