hello world
hello world

Reputation: 211

Autwire is Kafka Configuration

I am not able to autowire MyRestcall in the MySerialiser class.

Its throwing NPE as MyRestcall isnt getting autowired.How cani autowire it.

MyRestcall is annoted with @Service annotation.

@Configuration
public class KakfaConfiguration {



@Bean
    public ProducerFactory<String, MyMessage> producerFactory() {
        Map<String, Object> config = new HashMap<>();
        
        

        config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, MySerializer.class);

}


import org.apache.kafka.common.serialization.Serializer;

public class MySerializer implements Serializer {
    
    

    @Autowired
    private MyRestcall myRestcall;
    
    
    
    @Override
    public byte[] serialize(String topic, Object data) {
        
            myRestcall.m1();
    
            return out.toByteArray();
        } catch (Exception e) {

Upvotes: 0

Views: 263

Answers (1)

Gary Russell
Gary Russell

Reputation: 174689

config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, MySerializer.class);

With this configuration, Kafka creates the serializer, not Spring.

You need to define MySerializer as a @Bean and pass it into the producer factory directly.

Upvotes: 1

Related Questions