Most Java developers are quite familiar with the Spring Framework. After all, it’s one of the most popular application development frameworks for Java. It popularized the concept of Dependency Injection (DI)/Inversion Of Control (IOC), which contributes to more loosely coupled applications. As a result, applications are both easier to maintain and unit test. Overall, the Spring Framework was a huge leap forward over scaffolding an enterprise-level application on your own; however, wiring together the myriad components made setting up a basic project remained a huge pain.
That’s where Spring Boot comes in. During Web-application development, we would need the jars that we want to use, which versions of the jars to use, and how to connect them together. All Web applications have similar needs—for example, Spring MVC, Jackson Databind, Hibernate core, and Log4j (for logging). We also had to choose compatible versions of all these jars. Spring Boot decreases all of this complexity by wiring the components together using AutoConfiguration and also takes care of all the dependencies that your application requires, putting your application in a runnable state that much faster. For each project, Spring Boot:
- Auto-configures it with the Dispatcher Servlet, if the Spring jar is in the class path.
- Auto-configures the datasource, if Hibernate jar is in the class path.
- Gives us a pre-configured set of Starter Projects to be added as a dependency in our project.
In today’s article, we’ll be using a Spring Boot Starter to get a basic Web app up-and-running in no time!
What You’ll Need
Being a Java framework, you’ll need the JDK 1.8 or later. Spring Boot uses Gradle 4+ or Maven 3.2+ to build our project, so you’ll want to install that as well. Besides those, you can use whichever text editor or IDE you like!
Project Setup
There is a Getting Started GitHub Repository where you can download a basic project. Just unzip the archive on your workstation or device. Alternatively, you can clone the repo using Git:
git clone https://github.com/spring-guides/gs-spring-boot.git
The project structure should look like this (see Figure 1):
Figure 1: The project structure
There’s also a “complete” directory that you can check future coding efforts against.
Building the Project
As mentioned earlier, you can build your Spring Boot projects by using either Gradle or Maven. For this tutorial, we’ll focus on Gradle.
Both the initial and complete directories contain a build.gradle file. It contains a combination of declarative and imperative statements that define a project and tasks. The Spring Boot Gradle plug-in builds your project according to the instructions contained within the build.gradle file.
buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle- plugin:2.0.5.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' bootJar { baseName = 'gs-spring-boot' version = '0.1.0' } repositories { mavenCentral() } sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile("junit:junit") }
To build the project:
- “cd” into gs-spring-boot/initial.
- Issue the following command to start the build process. (If you’re in a PowerShell console, replace the double ampersands with a semi-colon to separate the two individual commands.):
gradlew build && java -jar build/libs/gs-spring- boot-0.1.0.jar
You should see output similar to Figure 2 in the console:
Figure 2: A successful build - To bring up the app in the browser, navigate to http://localhost:8080/. You should see the greeting depicted in Figure 3:
Figure 3: The greeting message
Conclusion
Although the main focus of this tutorial was to illustrate how quick and easy it is to get started with Spring Boot, there’s a lot more to the framework than that; it includes several built-in endpoints, a Git commit ID that’s exposed through the “/info” actuator endpoint, as well as profiles that make it easy to define different configurations for different environments. We’ll explore these—and more—in future articles.