Developing applications with Spring Boot

The recommended way to enable Spring Boot in your project is by using a dependency management system. Here, you can see a short snippet of how to include appropriate artifacts in your Maven and Gradle projects. Here is a sample fragment from the Maven pom.xml:

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

With Gradle, we do not need to define parent dependency. Here's a fragment from build.gradle:

plugins {
id 'org.springframework.boot' version '1.5.7.RELEASE'
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.5.7.RELEASE")
}

When using Maven, it is not necessary to inherit from the spring-boot-starter-parent POM. Alternatively, we can use the dependency management mechanism:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Now, all we need is to create the main application class and annotate it with @SpringBootApplication, which is an equivalent to three other annotations used together—@Configuration, @EnableAutoConfiguration, and @ComponentScan:

@SpringBootApplication
public class Application {

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

}

Once we have the main class declared and spring-boot-starter-web included, we only need to run our first application. If you use a development IDE, such as Eclipse or IntelliJ, you should just run your main class. Otherwise, the application has to be built and run like a standard Java application with the java -jar command. First, we should provide the configuration that is responsible for packaging all dependencies into an executable JAR (sometimes called fat JARs) during application build. This action would be performed by spring-boot-maven-plugin if it is defined in the Maven pom.xml:

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

The sample application does nothing more than start a Spring context on the Tomcat container, which is available on port 8080. The fat JAR is about 14 MB in size. You can easily, using an IDE, check out which libraries are included in the project. These are all basic Spring libraries, such as spring-core, spring-aopspring-context; Spring Boot; Tomcat embedded; libraries for logging including Logback, Log4j, and Slf4j; and Jackson libraries used for JSON serialization or deserialization. A good idea is to set the default Java version for the project. You can easily set it up in pom.xml by declaring the java.version property:

<properties>
    <java.version>1.8</java.version>
</properties>

We can change the default web container just by adding a new dependency, for example, to the Jetty server:

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>