The complete code for this application:
https://github.com/mtrojahn/spring-boot-console-app
In this example I'll try to show how to create a simple console application to print out the traditional "Hello World" message using spring-boot on a Maven project. I'll try to keep it as simple as possible for now and I'll most likely use this code as base for new posts.
Let's start with the pom.xml file.
4.0.0 com.mtrojahn spring-boot-console-app 1.0-SNAPSHOT jar UTF-8 1.8 com.mtrojahn.boot.Application org.springframework.boot spring-boot-starter-parent 1.3.0.RELEASE org.springframework.boot spring-boot-starter org.apache.maven.plugins maven-compiler-plugin 3.3 1.8 org.apache.maven.plugins maven-surefire-plugin 2.19 true org.springframework.boot spring-boot-maven-plugin repackage
This is a simple console application so, for now, we only need "spring-boot-starter" as dependency.
You can also notice that I included a few plugins. The most important for now is the spring-boot-maven-plugin which will allow us to run the project with the "mvn spring-boot:run" Maven directive.
And now the main class:
@SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder() .sources(Application.class) .bannerMode(Banner.Mode.OFF) .run(args); Application app = context.getBean(Application.class); app.start(); } private void start() { System.out.println("Hello World!"); } }
That's it.
The annotation @SpringBootApplication is a convenience annotation that implies a few others like @Configuration, @EnableAutoConfiguration and @ComponentScan. In more complex projects you most likely will declare each one individually to set a few parameters.
Now just execute the "mvn spring-boot:run" directive to run your application or "mvn package" to create a executable JAR file.
No comments:
Post a Comment