Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Spring to create a console application? [closed]

Currently researching this topic. Is it possible to use the benefits of Spring to form a Console application? I mean, with scopes, autowiring and etc(found a bunch of examples of a basic "Set the context, init and go", but nothing more)?

Can't find any tutorials or helpful information on this topic.

like image 972
Draaksward Avatar asked Nov 13 '17 13:11

Draaksward


2 Answers

CommandLineRunner example. If you run this Spring Boot, the run method will be the entry point.

package com.mkyong;

import com.mkyong.service.HelloMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import static java.lang.System.exit;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

    @Autowired
    private HelloMessageService helloService;

    public static void main(String[] args) throws Exception {

        //disabled banner, don't want to see the spring logo
        SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);

    }

    // Put your logic here.
    @Override
    public void run(String... args) throws Exception {

        if (args.length > 0) {
            System.out.println(helloService.getMessage(args[0].toString()));
        } else {
            System.out.println(helloService.getMessage());
        }

        exit(0);
    }
}

Details

like image 65
Grigoriev Nick Avatar answered Sep 28 '22 21:09

Grigoriev Nick


Here are links to the Spring docs:
3. Developing Spring Shell Applications
4. Simple sample application using the Spring Shell

like image 36
Ofer Skulsky Avatar answered Sep 28 '22 21:09

Ofer Skulsky