Class PicocliSpringFactory

  • All Implemented Interfaces:
    CommandLine.IFactory

    public class PicocliSpringFactory
    extends Object
    implements CommandLine.IFactory
    PicocliSpringFactory is a CommandLine.IFactory implementation that looks up classes in a given org.springframework.context.ApplicationContext. This allows picocli subcommands (and other objects that are instantiated as needed) to have javax.inject annotations that will be populated by Spring's dependency injection framework.
    Since:
    4.0.0
    • Constructor Detail

      • PicocliSpringFactory

        public PicocliSpringFactory​(org.springframework.context.ApplicationContext applicationContext)
        Constructs a PicocliSpringFactory with the specified Application context, and picocli's default factory as the fallback factory for classes not found in the application context.
        Parameters:
        applicationContext - the application context to look up classes in; must be non-null
      • PicocliSpringFactory

        public PicocliSpringFactory​(org.springframework.context.ApplicationContext applicationContext,
                                    CommandLine.IFactory fallbackFactory)
        Constructs a PicocliSpringFactory with the specified Application context, and the specified fallback factory for classes not found in the application context.
        Parameters:
        applicationContext - the application context to look up classes in; must be non-null
        fallbackFactory - the factory used to instantiate classes that are not found in the specified application context; must be non-null
        Since:
        4.7.0
    • Method Detail

      • create

        public <K> K create​(Class<K> clazz)
                     throws Exception
        Description copied from interface: CommandLine.IFactory
        Returns an instance of the specified class.
        Specified by:
        create in interface CommandLine.IFactory
        Type Parameters:
        K - the type of the object to return
        Parameters:
        clazz - the class of the object to return
        Returns:
        the instance
        Throws:
        Exception - an exception detailing what went wrong when creating or obtaining the instance