Class MicronautFactory

java.lang.Object
io.micronaut.configuration.picocli.MicronautFactory
All Implemented Interfaces:
AutoCloseable, picocli.CommandLine.IFactory

public class MicronautFactory extends Object implements picocli.CommandLine.IFactory, AutoCloseable
An ApplicationContext-based implementation of the picocli IFactory interface.

Specifying this factory when instantiating a CommandLine or invoking the CommandLine.run or CommandLine.call methods allows picocli to leverage Micronaut dependency injection.

Closing this factory will close the underlying ApplicationContext.

Since:
1.0
Author:
Remko Popma
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a MicronautFactory with the result of calling ApplicationContext.run("cli").
    MicronautFactory(io.micronaut.context.ApplicationContext applicationContext)
    Constructs a MicronautFactory with the specified ApplicationContext.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the underlying ApplicationContext.
    <K> K
    create(Class<K> cls)
    Delegates to the ApplicationContext to either find or instantiate a bean of the specified type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MicronautFactory

      public MicronautFactory()
      Constructs a MicronautFactory with the result of calling ApplicationContext.run("cli").
    • MicronautFactory

      public MicronautFactory(io.micronaut.context.ApplicationContext applicationContext)
      Constructs a MicronautFactory with the specified ApplicationContext.
      Parameters:
      applicationContext - the context to use to look up or instantiate beans
  • Method Details

    • create

      public <K> K create(Class<K> cls) throws Exception
      Delegates to the ApplicationContext to either find or instantiate a bean of the specified type.
      Specified by:
      create in interface picocli.CommandLine.IFactory
      Type Parameters:
      K - the type of the bean to return
      Parameters:
      cls - the class of the bean to return
      Returns:
      an instance of the specified class
      Throws:
      io.micronaut.context.exceptions.NoSuchBeanException - if no bean of the specified type exists
      Exception - if a problem occurred during lookup or instantiation
    • close

      public void close()
      Closes the underlying ApplicationContext.
      Specified by:
      close in interface AutoCloseable