Spring Boot is a work of art.

And yes, you should definitely believe the hype. It really is that good. Using Spring Boot, teams with significantly less experience in Java and Spring framework can also start delivering value with a little training.

Spring Boot takes away most of the challenges associated with normal Spring app development. However, having said that, Spring Boot is an inherent part of the overall Spring framework.

It’s primary objective is to bring Rapid Application Development to the world of Spring.

And how does it achieve that?

The concept is pretty simple. Spring Boot provides an opinionated framework to do stuff by means of starter templates. These templates make it exceedingly easy to bootstrap the application.

The best part, however, is that all of it works flawlessly.

So let’s get started with Spring Boot.

Creating a Spring Boot Application

The best way to create a new Spring Boot Application is to use https://start.spring.io

This is what is more commonly known as the Spring Initializr. It’s also my favorite place on the internet. It helps you cook up a working Spring Boot application in almost no time.

On the page that would open, you need to fill a few details. In the field Group, you can enter the high-level package name. This could be your domain. Artifact could be the name of your application.

You can click on Switch to Full Version to see all the details you can enter

I have entered the Group as com.favtuts.demo and Artifact as spring-boot-starter-app. See below image:

Note that we have selected Maven Project at the top. You could also go for Gradle. In the languages dropdown, you could opt for Kotlin or Groovy. We have selected Java.

You can also play around with the Spring Boot version. At the time of writing this, the latest stable version was 2.1.1 which is what I have selected.

Rest of the details are pretty self-explanatory. And I would not recommend changing anything if you are just starting out with Spring Boot.

The next important step is to select the various packages or features I want in the application. This step is similar to selecting the dependencies in a typical maven or gradle project. In the dialog box, you can scroll down to see the entire list of curated dependencies available.

For the purpose of this post, we would select a few important dependencies like Web, Rest Repositories, JPA, H2, Actuator. We would get into the details of these dependencies later.

At this point, we can now click on the big green button that says Generate Project. Alternatively, you can press Alt + Enter. The project will be downloaded to your computer.

Running the Application

Running the application is pretty simple. Unzip the downloaded project to a suitable place in your computer’s file system. Then, open it in your favorite IDE.

My favorite IDE for Java is IntelliJ from JetBrains. And I wouldn’t hesitate in recommending the same to you. A community edition is available at this location.

Once you open the project, it might take a moment to pull all the dependencies and build the overall project structure. You can grab a coffee during this time.

Once everything is setup, you could simply run the project using IntelliJ’s maven plugin.

The simplest command to run it is clean package spring-boot:run

Apply and click OK and then Run the application using the play button that appears near the top-right area of the editor.

Once the build completes successfully, you will be able to see the application start up on port 8080 (unless you aren’t using that port for something else).

You can now check whether your application is actually running successfully by visiting http://localhost:8080/actuator/health. If everything is fine, you would see a message saying {“status”:”UP”} displayed in the browser window.

With this, we have successfully setup a new Spring Boot application. Now we can work on this application and add some real functionality. But before that we need to understand what happens under the hood when you use Spring Boot.

The Dependencies

A Spring Boot project will usually have several dependencies. If you’ve used maven, you can see those dependencies in the pom.xml file of your project.

To start off, the most important part is the spring-boot-starter-parent. This is a special starter provided by Spring Boot. It’s job is to provide useful Maven defaults and a dependency management section. As a result, it allows us to omit version numbers in other dependencies.

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

Next are the actual dependencies as shown below:

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-rest</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

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

Notice that we don’t put version tags in these dependencies.

A quick walk-through of these dependencies is like this:

Spring Boot Starter Web – This helps enable your Spring Boot application as a web application. It also brings in Tomcat that will be used to serve your application.

Spring Boot Starter Data JPA – This brings support for JPA and Hibernate as well as configures the data sources available on the classpath.

H2 – H2 is an in-memory database. It is very useful for quickly iterating through changes during development phase. When Spring sees H2 on the classpath, it will configure a data source for us. It will also wire up the datasource in our application context.

Actuator – Spring Boot actuator exposes certain endpoints from your application. These endpoints allows us to ask questions about the health of our application and also query for other run time statistics. This feature comes in very handy when we have to implement automated health-checks for our application.

Spring Boot Startup – The Main Class

Every Spring Boot application has a main class. This is the class that deals with the Spring Boot startup process.

The main class of our application looks as below.:

@SpringBootApplication
public class SpringBootStarterApplication {

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

}

Notice the @SpringBootApplication annotation. Behind the scenes, Spring does a lot of heavy-lifting just because of that one annotation.

The @SpringBootApplication is equivalent to a combination of the below annotations:

@Component – This tags the class as a source of bean definitions that you might want to wire up into the application context.

@EnableAutoConfiguration – This annotation instructs Spring Boot to start adding other beans to the context depending on the property or classpath settings. For example, if spring-webmvc is on the classpath, this annotation will activate key behavior such as setting up a DispatcherServlet.

@ComponentScan – This instructs Spring to look for other components, configuration classes and services in the same package heirarchy. As a result of this, you can declare beans in your classes that will be wired to the application context during startup.

Finally, there is the main() method. This method, in turn, calls the run() method of SpringApplication class to launch the application.

And that’s about it. No XML Configuration required. No web.xml file.

As you can see, Spring Boot allows you to write Spring Applications using pure Java configuration. And even with that, you don’t have to write any infrastructure setup logic. Everything is taken care of by Spring Boot.

In the next post, we will start off by setting up H2 database for our Spring Boot application.

Download Source Code

$ git clone https://github.com/favtuts/java-spring-boot-microservices/tree/spring-boot-startup
$ cd java-spring-boot-microservices/sources/spring-boot-starter-app
$ mvn clean package spring-boot:run

Leave a Reply

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