title: How to inject beans and create custom beans in Micronaut cli application
body: <h2>I want to inject beans in micronaut cli application</h2>
  
  <h1>Example: Below is my Command Class</h1>
  
  <pre><code>@command(name = "com/utils", description = "..",
  mixinStandardHelpOptions = true, header = {..})
  public class UtilityCommand implements Runnable {
  
  @Inject
  SomeBean somebean;
  
  public void run() {
  somebean.method1();
  }
  }
  
  # Now I want to create Singleton bean using below syntax #
  
  @Singleton
  public class SomeBean {
  
   @Inject RxHttpClient client;
  
   void method1(){
  client.exchange(); // Rest call goes here
  }
  
  }
  </code></pre>
  
  <h1>I tried creating Factory class as per documentation(<a href="https://docs.micronaut.io/latest/api/io/micronaut/context/annotation/Factory.html" rel="nofollow noreferrer">https://docs.micronaut.io/latest/api/io/micronaut/context/annotation/Factory.html</a>) and created bean, but no luck</h1>
  
  <p>@Factory
   public class MyFactory {</p>
  
  <pre><code> @Bean
   public SomeBean myBean() {
       new SomeBean();
   }
  </code></pre>
  
  <p>}</p>
  
  <h2>I came to this when I ran my tests.</h2>
  
  <p>Simple testcase to check verbose output ##</p>
  
  <pre><code>public class UtilityCommandTest {
  
  @test
  public void testWithCommandLineOption() throws Exception {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  System.setOut(new PrintStream(baos));
  
  try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {
      **ctx.registerSingleton(SomeBean.class,true);**
      String[] args = new String[] { "-v"};
      PicocliRunner.run(UtilityCommand.class, ctx, args);
      assertTrue(baos.toString(), baos.toString().contains("Hi!"));
  }
  }
  </code></pre>
  
  <h3>Im getting following exception</h3>
  
  <p>picocli.CommandLine$InitializationException: Could not instantiate class com.UtilityCommand: io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for field [someBean] of class: com.UtilityCommand</p>
  
  <p>Path Taken: UtilityCommand.someBean</p>