Spring Boot – Getting started

Spring Boot makes it easy to create Spring-powered applications and services with absolute minimum fuss. This is due to its dependencies management structure. Spring boot calls its intelligent build of dependencies the ‘Bill of material’ or the BOM. The spring developers made a list of different frameworks you may want to use in your application and match up their version which work well with each other. Got exmaple, spring mvc 4.2.3 works well with logback 1.1.3 or some other dependencies. By using this spring boot BOM, all of these matches has been setup for you. This saves us a lot of time trying to find our libraries that works well with each other.

I will be using maven here to build. Spring provided us a very easy way to use UI to create a project structure for our project using SPRING INITIALIZR (https://start.spring.io).

spring-initializer

This will download a zip containing our project structure. Of course you can build using your IDE like eclipse, InlliJ etc. There is another tool provided by spring team is using CLI (command line interface). Lets import in eclipse and see project structure and pom.xml

spring-bbot-project-structure


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mynotes</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

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

Notice the parent tag in the pom. This parent brought in all of the dependency management. It also brings us other defaults like plugins , java minimum support version, resource filtering and so on. Check on the dependency hierarchy tab to see what version of dependency came in. Since we used spring web starter pack (no version given) , you should see spring webmvc, core and other related dependencies like for mvc spring automatically added an embedded tomcat container. These default could be changed like using jetty instead etc.

Lets create our HomeController.java


package com.mynotes;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

@RequestMapping("/")
public String index() {
return "Spring Boot!";
}

}

Lets see the Application.java class.


package com.mynotes;

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

@SpringBootApplication
public class Application {

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

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
  • @ComponentScan tells Spring to look for other components, configurations, and services in the the mynotes package, allowing it to find the HomeController.

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application.

This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.

To run go the the pom.xml in terminal and type


mvn spring-boot:run

This will start the application which by defaults runs on 8080 port. You could also directly run the main method from eclipse (Make sure to build first, mvn clean package or mvn clean install).Checking

spring-boot-run1

So far we have been using the default setup spring provide us. What if we want to change some of the defaults, like change the port or add a context-root to our app. This is where application.properties files comes in. This file is placed at the root of your class path, spring boot will load it and apply any of the property configuration given here. This property can change from environment to environment like dev, stage, prod etc. Spring boot provides an easy way to handle these changes as you move from one environment to another. In addition to your standard application.properties, you can define a property file that may have a profile embedded in the name of the file. For example:

application-{profile}.properties – application-dev.properties

To tell spring which to load by using -Dspring.profiles.active=dev in the arguments.

What can you set here could be seen from docs

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

For exmaple – server.port , server.servlet-path etc.

If you could see the pom.xml the packaging is by default a jar. This is because, you could run your application via main and because we have an embedded tomcat container it will handle http request. Ofcourse this packaging could be change to a war and then we can deploy it on tomcat server. So why do you want a containerless deployment and simple run you app as a plain java program?

spring-boot-container-deployments

With a container deployment, you bundle up your application and deploy on a server somewhere. There are many steps that needs to be done. First the container needs to be setup for each environment. Next you need  to have some deployment descriptors (web.xml) so that the container understands how to serve up your application. Many of the times, some of the configuration are external to the application. Lets see when the container is embedded

spring-boot-containerless-deployments

With this your app will run anywhere java is setup correctly especially on clouds. You can simply run your application jar file and it starts running. You need not find our hosting sites that can run tomcat/jboss etc. Every configuration is internal to application.

%d bloggers like this: