title: The correct way for creation of KafkaTemplate in spring boot
body: <p>I try configure <strong>apache kafka</strong> in <strong>spring boot</strong>  application. I read this <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-messaging.html#boot-features-kafka" rel="noreferrer">documentation </a> and follow the steps:</p>
  
  <p>1) I add this lines to <code>aplication.yaml</code>:</p>
  
  <pre><code>spring:
    kafka:
      bootstrap-servers: kafka_host:9092
      producer:
        key-serializer: org.apache.kafka.common.serialization.StringDeserializer
        value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
  </code></pre>
  
  <p>2) I create new Topic:</p>
  
  <pre><code>    @Bean
      public NewTopic responseTopic() {
          return new NewTopic("new-topic", 5, (short) 1);
      }
  </code></pre>
  
  <p>And now I want use <code>KafkaTemplate</code>:</p>
  
  <pre><code>private final KafkaTemplate&lt;String, byte[]&gt; kafkaTemplate;
  
  public KafkaEventBus(KafkaTemplate&lt;String, byte[]&gt; kafkaTemplate) {
      this.kafkaTemplate = kafkaTemplate;
  }
  </code></pre>
  
  <p>But Intellij IDE highlights:</p>
  
  <p><a href="https://i.stack.imgur.com/LY8Ng.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LY8Ng.png" alt="enter image description here"></a></p>
  
  <p>To fix this I need create bean:</p>
  
  <pre><code>@Bean
  public KafkaTemplate&lt;String, byte[]&gt; myMessageKafkaTemplate() {
      return new KafkaTemplate&lt;&gt;(greetingProducerFactory());
  }
  </code></pre>
  
  <p>And pass to constructor propirties <code>greetingProducerFactory()</code>:</p>
  
  <pre><code>@Bean
  public ProducerFactory&lt;String, byte[]&gt; greetingProducerFactory() {
      Map&lt;String, Object&gt; configProps = new HashMap&lt;&gt;();
      configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka_hist4:9092");
      configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
      configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
      return new DefaultKafkaProducerFactory&lt;&gt;(configProps);
  }
  </code></pre>
  
  <p>But then what's the point of setting in <strong>application.yam</strong>l if I need create ProducerFactory manual?</p>