By default, when we create a Spring Boot project, it will run as a web application with an embedded web container such as Tomcat, Jetty… So in case we want to run the Spring Boot project as a normal Java console, how do we do it? In this tutorial, I will guide you how to create Java application console with your Spring Boot project.
First of all, you also create a normal Spring Boot project using the Spring Tool Suite IDE or the Spring Initializr Web, but note that in the dependencies section, you do not select anything:
My example is as follows:
To run this example as a Java console application, add the implementation interface CommandLineRunner to the main class of the Spring Boot application, in my example SpringBootConsoleApplication class:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.springboot; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(SpringBootConsoleApplication.class, args); } } |
This CommandLineRunner interface has a method that we must implement, which is the run() method. This is also the method that allows us to execute code under the console.
For example, now I want to print a message in the console, I will code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.huongdanjava.springboot; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(SpringBootConsoleApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println("Hello from Huong Dan Java"); } } |
Result:
Let’s say we now have a HelloWorld bean with the following content:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.springboot; import org.springframework.stereotype.Component; @Component public class HelloWorld { public void say() { System.out.println("Hello, Khanh"); } } |
Then, we can use the HelloWorld bean as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.huongdanjava.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner { @Autowired private HelloWorld helloWorld; public static void main(String[] args) { SpringApplication.run(SpringBootConsoleApplication.class, args); } @Override public void run(String... args) throws Exception { helloWorld.say(); } } |
Result: