Introduction to Spring Boot
Application development is very important as it helps in our day-to-day tasks. For example, a food ordering application which supports in ordering online have to use a frontend UI, backend (server-side programming), and a database to persist data. Java has been a popular choice for many programmers since the mid 90's for designing software and has hence, seen a lot of frameworks getting developed for the ease of software development using Java.
To make the development easy and effective several frameworks were introduced such as Struts, Spring and ORM tools such as Hibernate, Toplink, etc. for the database operations.
Spring has evolved a lot in recent years with many new modules such as spring-boot, spring-security, spring-cloud, etc. In this series of tutorials, we will learn the spring-boot framework and the various features it provides, making it a popular choice for application development. Spring boot was developed by the PIVOTAL team.
Pre-requisite:
- Knowledge on java programming.
- Hands-on REST web-services
- Dependency management tools such as Maven or Gradle would be an added advantage.
Advantages of Spring Boot:
-
The primary advantage of spring-boot is enabling auto-configuration for the project. This reduces the burden on programmers and saves a lot of time.
-
It makes use of annotations which are very simple and replace the typical XML based configuration setup in spring MVC.
-
Provides an embedded servlet container and helps in easily bootstrapping (quick run) the application that is production-ready.
-
It enables creation of fat Jar for spring applications which has all the dependencies present in it and hence you can directly run it.
-
Microservices architecture can be built effectively with the spring-boot framework and it also provides concepts such as spring-boot-starter for the dependency management.
Classpath Dependencies:
Setting up a project requires a lot of external libraries (core Java and other related frameworks). As mentioned previously, spring-boot enables auto-configuration which is dependent on the available JARs in the classpath. We will look at the auto-configuration when we start setting up the project.
spring-boot-starter helps in managing all the dependencies effectively unlike spring-MVC where there is no concept of starters.
What is spring-boot-starter?
Starters enable effective dependency management for your spring based project. For example, if we want to use hibernate as the ORM tool, we need the hibernate maven script (To be included in pom.xml file of the project) as follows.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
Spring-boot provides starters such as spring-boot-starter-data-jpa which provides the above mentioned hibernate dependencies as well as the spring-data-jpa dependencies when included in the maven project.
In short, starters are the composition of multiple dependencies which are resolved easily and reduces the task of including each dependency manually by specifying their names in your pom.xml (for maven project) or ivy.xml (for IVY/ANT project) or build.gradle file (for Gradle project). Some of the examples are:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
Observe that the above starter scripts have no version mentioned unlike in the code above as the version gets imported from the spring-boot-starter-parent which will be discussed further.
What is application.properties?
This file helps us in managing the configuration of the application using simple properties. Depending on the configuration provided, spring-boot auto-configures some beans (Objects which are directly managed by spring and are initialized at the time of application launch).
It also helps in enabling other configuration which is provided by the user. For example, the default port for embedded tomcat is 8080 which can be changed by the property server.port mentioned in the application.properties file. The below property runs the tomcat on port 9000.
server.port=9000
The above properties file can be replaced with yml file (application.yml). Spring-boot enables us to maintain multiple properties file for different environments. We can choose the required configuration at the time of application launch. We will go through this topic in detail when we start developing the application.
Conclusion: In this tutorial, we got introduced to spring-boot and have seen its importance and the advantages it offers for easier development.