Command Line Applications with Spring

It was quite a surprising for me to find that Spring already have built-in support for CLI applications – I was particularly interested in the argument parsing.

I’ve created a small spring demo project on github.

You can try to start it by java -jar target/cli-demo-jar-with-dependencies.jar --url=alexshabanov.com

The code is quite simple and self evident:

public final class CliDemoApp {
    public static final Logger LOG = LoggerFactory.getLogger(CliDemoApp.class);

    public static final String URL_PARAM_KEY = "url";


    public static void main(String[] args) {
        LOG.trace("CLI demo app");

        // should use parameterless constructor as the other one invoke refresh which we certainly don't want
        // as it automatically trigger property injection and our CLI property is not ready yet
        final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // setup configuration
        applicationContext.register(Config.class);

        // add CLI property source
        applicationContext.getEnvironment().getPropertySources().addLast(new SimpleCommandLinePropertySource(args));

        // setup all the dependencies (refresh) and make them run (start)
        applicationContext.refresh();
        applicationContext.start();

        try {
            applicationContext.getBean(Runnable.class).run();
        } finally {
            applicationContext.close();
        }
    }

    @Configuration
    public static class Config {
        @Autowired
        private Environment environment;

        @Bean
        public Runnable myBean() {
            return new MyBean(environment.getProperty(URL_PARAM_KEY, "http://127.0.0.1:8080"));
        }
    }

    public static final class MyBean implements Runnable {
        private final String url;

        public MyBean(String url) {
            this.url = url;
        }

        @Override
        public void run() {
            LOG.info("MyBean run with url = {}", url);
        }
    }
}

3 thoughts on “Command Line Applications with Spring

  1. Couldn’t you have injected the URL straight onto the MyBean? Would AnnotationConfigApplicationContext do that out of the box?

    @Value(value = “${url}”)
    private String url;

  2. No, unfortunately it won’t work.
    As of versions 3.0.x, 3.1.x spring property binding has some issues with value annotation – it works for XML configs but it unpredictably behaves with annotation config-driven apps. There are some workarounds for that, but the code you mentioned in your post will not work, at least I don’t know straight and simple approach to how to fix that.

  3. I actually made it work on my case by doing the following (with Spring 4.1.6.RELEASE):

    @Configuration
    public static class Config {
    @Bean
    public Runnable myBean(@Value(“${url}”) String url) {
    return new MyBean(url);
    }
    }

    On your demo code however, it does not seem to work though I do not see any difference with what I did on my own code.

Leave a Reply

Your email address will not be published. Required fields are marked *