Spring Boot – Quickstart

In the following post we are going to look at setting up a very very simple spring boot application that just runs, with only a few lines of code. This is a quick start version for setting an app in less than a minute. 

Requisites

  • Java 1.8
  • Spring Boot 1.3.3
  • Maven 4
  • IntelliJ Idea/Eclipse

Lets go

Advertisements

Create new maven quickstart project

Add the following dependencies to the pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>1.3.3.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
  <version>1.3.3.RELEASE</version>
</dependency>

and the following build section

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

Now create an empty class under the src folder

Add the following annotation before the class declaration

@SpringBootApplication

What this does is it provides defaults from the Spring boot dependencies for things like web server, jdbc, jts etc, based on the pom xml definitions.

Then call the run method of SpringApplication class passing in the current class as a parameter.

The complete source code is below

package com.atechref.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Spring boot app
 * @author APeter
 */
@SpringBootApplication
public class App {

    public static void main( String[] args ) {
        SpringApplication.run(App.class, args);
    }
}

That is all the code for this example,

Now run

              mvn clean install

in the IDE and this would download all the maven dependencies and build the project.

In a command window cd to the project folder and enter

             mvn spring-boot:run

This would download all the run time dependencies, start the embedded tomcat server and deploy our app.

When the following is visible in the console then we know the app is deployed successfully.

2016-04-13 22:40:14.934 INFO 4052 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-04-13 22:40:14.944 INFO 4052 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2016-04-13 22:40:15.304 INFO 4052 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-13 22:40:15.310 INFO 4052 --- [ main] com.atechref.spring.App : Started App in 4.127 seconds (JVM running for 6.922)

We now have an app running in less than a minute with absolutely 0 lines of bean xml, one annotation and two dependencies in a pom file.

References:
http://docs.spring.io/spring-boot/docs/1.3.3.RELEASE/reference/html/

Advertisements

Leave a Reply

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