title: How to use @Autowired MongoTemplate in multiple classes in spring boot
body: <p>Hi I'm relatively new to spring boot and I'm trying to use the mongotemplate
  using autowired here's my class</p>
  
  <pre><code>@SpringBootApplication
  public class Test implements CommandLineRunner {
     public static void main(String[] args) {
         SpringApplication.run(ProducerConsumerApplication.class, args).close();
     }
  
    @Autowired
    private MongoTemplate mongoTemplate;
  
  
  
    @Override
    public void run(String... strings) throws Exception {
      new Myclass().insert();
    }
  </code></pre>
  
  <p>This is how my application.yml looks like. </p>
  
  <pre><code>spring:
    kafka:
      bootstrap-servers: 192.168.155.100:9092
      consumer:
        group-id: foo
    data:
      mongodb:
        host: 192.168.155.100
        port: 27017
        database: test
  </code></pre>
  
  <p>I am trying to use the mongo template in Myclass and I am calling it from the Test class but the mongotemplate is null. How should I be doing this? Any help is appreciated</p>