-
Notifications
You must be signed in to change notification settings - Fork 434
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 not used when calling parseWithHandler #658
Comments
Ouch! It looks like the setup explained in the manual only covers top-level commands, not subcommands. I think this is because picocli instantiates subcommands directly, while it should get subcommand instances from the Spring What is needed is an implementation of picocli's IFactory that takes beans from the Spring Do you think you can create this factory yourself? Picocli 4.0 has plans to improve Spring Boot support, but I cannot give you any time lines. |
Hi!
I'll fork the project and give it a try :-).
Cheers!
…On Sun, 7 Apr 2019, 01:51 Remko Popma, ***@***.***> wrote:
Ouch! It looks like the setup explained in the manual only covers
top-level commands, not subcommands. I think this is because picocli
instantiates subcommands directly, while it should get subcommand instances
from the Spring ApplicationContext.
What is needed is an implementation of picocli's IFactory
<https://picocli.info/apidocs/picocli/CommandLine.IFactory.html> that
takes beans from the Spring ApplicationContext instead of instantiating
them directly. This factory can be passed in to the CommandLine
constructor. (Something similar to MicronautFactory
<https://github.com/micronaut-projects/micronaut-core/blob/master/configurations/picocli/src/main/java/io/micronaut/configuration/picocli/MicronautFactory.java>
for the Micronaut dependency injection container.)
Do you think you can create this factory yourself? Picocli 4.0 has plans
<#496> to improve Spring Boot
support, but I cannot give you any time lines.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#658 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGDmmmPSYDy0ihWaeNDK-nwIewjs-5Y-ks5veTL7gaJpZM4cgE7T>
.
|
See `ApplicationContextAwarePicocliFactory` fixes #9 and remkop/picocli#658 related to #7 --- [x] Impl [x] Test [ ] Sample [ ] Doc
See `ApplicationContextAwarePicocliFactory` fixes #9 and remkop/picocli#658 related to #7 --- - [x] Impl - [x] Test - [ ] Sample - [x] Doc
See `ApplicationContextAwarePicocliFactory` fixes #9 and remkop/picocli#658 related to #7 --- - [x] Impl - [x] Test - [x] Sample - [x] Doc
See `ApplicationContextAwarePicocliFactory` fixes #9 and remkop/picocli#658 related to #7 --- - [x] Impl - [x] Test - [x] Sample - [x] Doc
As I understand there is nothing more to be done here? |
@dupirefr There is work in progress in the kakawait/picocli-spring-boot-starter project to update that project to picocli 3.x. My goal is to work together with @kakawait to bring full Spring support into the picocli project as a picocli module (or maybe two modules, one It would be great if you would be able to try this functionality as it matures, so we can get early feedback to help flush out issues before 4.0. |
@dupirefr Meanwhile, I believe it should be possible to address this with a custom public class PicocliSpringFactory implements CommandLine.IFactory {
private final ApplicationContext applicationContext;
public PicocliSpringFactory(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public <K> K create(Class<K> aClass) throws Exception {
try {
return applicationContext.getBean(aClass);
} catch (Exception e) {
try {
return aClass.newInstance();
} catch (Exception ex) {
Constructor<K> constructor = aClass.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
}
}
}
}
CommandLine cmd = new CommandLine(new MyApp(), new PicocliSpringFactory(appContext)); |
Hi @remkop ! I copied the code from the DefaultFactory in the catch block and now it's working. Of course, copying it is not the long-term solution I think, but as DefaultFactory is private I couldn't do something smarter. I was thinking maybe some kind of decorator pattern could work if the DefaultFactory was made available (maybe only via the static factory method that I saw in the code). We could then do something like:
What do you think? |
Absolutely agreed. I made the factory method public: #684 This was included in the 4.0-alpha-3 release just now: |
I like your idea of putting the |
Nice, thank you very much :-). It seems to work for now, though I didn't do extensive tests. |
…cli-spring-boot-starter For now I prefer the simplicity of having a single module. The [docs](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html#boot-features-custom-starter-naming) mention that if you only have one module that combines [the starter and auto-configuration], name it XXX-spring-boot-starter.
@dupirefr The I believe this fixes the issue reported in this ticket. Can you verify? This will be included in the upcoming picocli 4.0 release. |
Hi!
Sorry that I didn't get back at you sooner, I was pretty busy. If it's not
too late I'll try to have a look at this in the weekend.
Kind regards,
François DUPIRE
GSM : +32 (0)474/48 55 82
…On Wed, 10 Jul 2019 at 15:14, Remko Popma ***@***.***> wrote:
@dupirefr <https://github.com/dupirefr> The picocli-spring-boot-starter
module contains a PicocliSpringFactory and an auto-configuration class
for much improved Spring integration.
I believe this fixes the issue reported in this ticket. Can you verify?
This will be included in the upcoming picocli 4.0 release.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#658?email_source=notifications&email_token=ABQONGSBI3WM6GSNMM7GDG3P6XOBXA5CNFSM4HEAJ3J2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZTNJTQ#issuecomment-510055630>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQONGRVD5PMYGZMTLPCLQDP6XOBXANCNFSM4HEAJ3JQ>
.
|
Hi @remkop , I just tested the feature, it works like a charm! Thanks a lot for fixing that problem :-). Cheers! François |
Great! Thanks for the confirmation. |
Hi!
I'm trying to use the parseWithHandler method of CommandLine to delegate calls to my subcommands automatically, but it doesn't seem to work with SpringBoot (dependencies are not autowired).
Here is my Application class:
The commands I created:
And finally a service I intend to use:
When I try to execute the subcommand, it fails because the service is null. Is it not supported yet?
Thanks a lot!
The text was updated successfully, but these errors were encountered: