Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Boot autoconfiguration #766

Closed
remkop opened this issue Jul 10, 2019 · 5 comments
Closed

Spring Boot autoconfiguration #766

remkop opened this issue Jul 10, 2019 · 5 comments
Milestone

Comments

@remkop
Copy link
Owner

remkop commented Jul 10, 2019

Working with the new picocli-spring-boot-autoconfigure module, I seem to have a problem getting the Spring Boot autoconfiguration to work for PicocliSpringFactory.

I expect this configuration to be able to produce the auto-configured factory:

// this does not work
@Configuration
@ComponentScan
public class MySpringApp implements CommandLineRunner {
    @Autowired
    IFactory factory;
    //PicocliSpringFactory factory; // this also did not work

    @Autowired
    MyCommand myCommand; // a @picocli.CommandLine.Command-annotated class

    @Override
    public void run(String... args) {
        new CommandLine(myCommand, factory).execute(args);
    }

    public static void main(String[] args) {
        SpringApplication.run(MySpringApp.class, args);
    }
}

What actually happens is that the application fails to start because the IFactory bean cannot be found in the application context. What am I doing wrong?

Note that the above does work if I @Import the factory.

// this does work
@Configuration
@ComponentScan
@Import(PicocliSpringFactory.class)
public class MySpringApp implements CommandLineRunner {
   // ... same as above

But if the application needs to explicitly @Import the factory, we are configuring manually, so what is the point of the auto-configuration? (And in fact, with the @Import statement, the application works even without the auto-configuration class...)

I cannot see anything wrong with the auto-configuration code, but showing it here for reference: the new picocli-spring-boot-autoconfigure module has the following auto-configuration class:

@Configuration
@ConditionalOnClass(CommandLine.class)
public class PicocliAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(IFactory.class)
    public IFactory picocliSpringFactory(ApplicationContext applicationContext) {
        return new PicocliSpringFactory(applicationContext);
    }
}

As per the docs, the jar that this class (and the PicocliSpringFactory class) are in has a META-INF/spring.factories file with the following content:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=picocli.spring.boot.autoconfigure.PicocliAutoConfiguration

Any idea what I'm doing wrong?

@remkop
Copy link
Owner Author

remkop commented Jul 10, 2019

The answer turned out to be simple: add the @EnableAutoConfiguration to the configuration...

// this does work
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MySpringApp implements CommandLineRunner {
   // ... same as above

@remkop remkop closed this as completed Jul 10, 2019
@rome-legacy
Copy link

rome-legacy commented Jul 27, 2019

Hi @remkop,

i'm also having this problem, but proposed solution does not work (also because i use @SpringBootApplication annotation like proposed in documentation)
i'm using spring boot starter 2.1.6 and picocli 4.0.1

2019-07-27 21:25:01.857  INFO 34487 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-27 21:25:02.019 ERROR 34487 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

Field factory in myApp required a bean of type 'picocli.CommandLine$IFactory' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'picocli.CommandLine$IFactory' in your configuration.

any suggestions?
kind regards

@remkop
Copy link
Owner Author

remkop commented Jul 28, 2019

@rome-legacy It’s hard to tell what could be the problem. Can you share your code?

@rome-legacy
Copy link

Hi @remkop ,
i could fix the problem. i was missing the picocli-spring-boot-starter module in my pom. maybe you add this detail to https://picocli.info/#_spring_boot_example.
or maybe it is just me, i was using the https://github.com/kakawait/picocli-spring-boot-starter before and was following his repository.

anyway, after adding the right dependency (https://github.com/remkop/picocli/tree/master/picocli-spring-boot-starter) it seems to work now. thx for your great library. if i will face any errors with spring boot, i'll let you know :-)

regards

remkop added a commit that referenced this issue Jul 29, 2019
…using `PicocliSpringFactory`; jline2 incompatibility with picocli's `interactive` password options.
@remkop
Copy link
Owner Author

remkop commented Jul 29, 2019

@rome-legacy Great to hear the problem was resolved.

I updated the user manual to mention the dependency: https://picocli.info/#_spring_boot_example
Thanks for the suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants