Console Application with Spring Boot: Difference between revisions
Jump to navigation
Jump to search
(4 intermediate revisions by the same user not shown) | |||
Line 14: | Line 14: | ||
dependencies { | dependencies { | ||
implementation('org.springframework.boot:spring-boot-starter') | implementation('org.springframework.boot:spring-boot-starter') | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Spring Boot Application Class= | =Spring Boot Application Class= | ||
<syntaxhighlight lang='java'> | |||
import org.springframework.boot.CommandLineRunner; | |||
import org.springframework.boot.SpringApplication; | |||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||
@SpringBootApplication | |||
public class MyCommandLineApplication implements CommandLineRunner { | |||
public static void main(String[] args) { | |||
SpringApplication.run(MyCommandLineApplication.class, args); | |||
} | |||
@Override | |||
public void run(String... args) throws Exception { | |||
System.out.println("running with " + Arrays.asList(args) + " ..."); | |||
new CommandLineLoop().run(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
=Simplest Command Line Loop Implementation= | |||
<syntaxhighlight lang='java'> | |||
package ...; | |||
import java.io.BufferedReader; | |||
import java.io.InputStreamReader; | |||
@SuppressWarnings("WeakerAccess") | |||
public class CommandLineLoop { | |||
private BufferedReader br; | |||
public CommandLineLoop() { | |||
this.br = new BufferedReader(new InputStreamReader(System.in)); | |||
} | |||
public void run() throws Exception { | |||
while(true) { | |||
System.out.print("> "); | |||
String line = br.readLine().trim(); | |||
if (line.isEmpty()) { | |||
continue; | |||
} | |||
if (line.toLowerCase().startsWith("exit")) { | |||
break; | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> |
Latest revision as of 17:59, 31 October 2018
External
Internal
Overview
Starter Dependencies
dependencies {
implementation('org.springframework.boot:spring-boot-starter')
}
Spring Boot Application Class
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyCommandLineApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(MyCommandLineApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("running with " + Arrays.asList(args) + " ...");
new CommandLineLoop().run();
}
}
Simplest Command Line Loop Implementation
package ...;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@SuppressWarnings("WeakerAccess")
public class CommandLineLoop {
private BufferedReader br;
public CommandLineLoop() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public void run() throws Exception {
while(true) {
System.out.print("> ");
String line = br.readLine().trim();
if (line.isEmpty()) {
continue;
}
if (line.toLowerCase().startsWith("exit")) {
break;
}
}
}
}