7 Things to Know Getting Started With Spring Boot by Ranga Karanam

Spring Boot is a tricky framework, so it is often tricky to understand. This tutorial will explain the basic components of Spring Boot, including the Starter project, automatic configuration, and Starter Parents, to help beginners understand and master Spring Boot.

Understand the Architecture of Spring Boot

Spring Boot contains these components:

  1. Spring Boot Starter project
  2. Spring Boot Starter Parent
  3. Automatic configuration

First, we will compare Spring Boot with Spring and Spring MVC to help understand what Spring Boot brings. Once you understand what Spring Boot has to offer, you’ll be better able to use it.

Spring Boot vs Spring MVC vs Spring

The most important thing about their relationship is:

Spring Boot is not in competition with Spring and Spring MVC, but in order to make better use of them.

The Spring framework

The most important feature of the Spring framework is Dependency Injection, and the core of all Spring modules is Dependency Injection and IOC-Inversion of Control.

With the proper use of dependency injection and inversion of control, loosely coupled applications can be developed easily and easily tested.

Spring MVC

Spring MVC provides Dispatcher servlets, ModelandViews, and View Resolvers that make it easy to write loosely coupled Web applications.

Spring Boot

One of the drawbacks of Spring, and Spring MVC, is that configuration is complex:

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<mvc:resources mapping="/webjars/**" location="/webjars/"/>
Copy the code

Spring Boot solves this problem with automatic configuration and the Starter Project. It also provides many features that make building production-ready applications easy and quick.

For a deeper comparison between Spring Boot and Spring and Spring MVC, read: Spring Boot vs. Spring MVC vs. Spring-How do they compare?

Spring Boot is automatically configured

Spring and Spring MVC have a lot of XMl and Java Bean configurations. Spring Boot solves this problem for us with a new idea:

Can we make the configuration smarter? When we add a Spring MVC JAR to our application, can we automatically configure some Java beans?

  • If you have Hibernate JAR packages in your classpath, the Data Source is automatically configured.
  • If you have Spring MAC jars in your classpath, how about the Dispatcher Servlet being automatically configured?

Spring Boot automatic configuration rules:

Spring Boot first checks two parts: the framework that exists in the classpath; The other is to apply existing configurations. Combining these two parts, Spring Boot provides the basic configuration required by these frameworks, which is Spring Boot’s automatic configuration.

For an in-depth explanation of automatic Configuration, read: What is Spring Boot Auto Configuration?

Spring Boot Starter project

The Spring Boot documentation describes the Starter project as follows:

The Starter project is a convenient set of dependency descriptions that can be referenced in our project. With the Starter project, we can get all the Spring and related technologies we need in a “one-stop shop,” rather than having to find various example code and copy the related dependency descriptions as before. For example, if we wanted to use Spring and JPA to link databases, all we had to do was introduce spring-boot-starter-data-jPA into our project and the project would be built.

Here is an example from the Spring Boot Starter Web:

If we want to develop a Web application or RESTful service, we can use the Spring Boot Starter Web directly.

When adding Spring Boot Starter Web to our project dependencies, we can see that many other dependencies have been added to the application, as shown in the screenshot:

It contains many of the common modules needed to develop a Web application. Using the Spring Boot Starter Web, we can easily introduce and use them:

  • Spring-core, beans, Context, AOP
  • Web MVC – Spring MVC
  • Jackson-json parsing library
  • Validation-hibernate Validator, Validation API
  • Embedded Servlet Container – Tomcat
  • Logging-logback, SLF4J

Almost all Web applications require these modules, and the Spring Boot Starter Web has packaged these dependencies. Developers don’t have to deal with these dependencies and their version-compatibility issues.

Spring Boot provides a number of such starter projects. And Spring Initializr supports all of them, including:

  • Spring-boot-starter-web-services: builds SOAP Web service applications
  • Spring-boot-starter-web: builds Web applications and RESTful services
  • Spring-boot-starter-test: Write unit tests and integration tests
  • Spring-boot-starter-jdbc: indicates a traditional JDBC application
  • Spring-boot-starter-hateoas: Add the Hateoas feature to applications to make services more RESTful
  • Spring-boot-starter-security: Uses Spring Security for authentication and authorization
  • Spring-boot-starter-data-jpa: Spring data JPA and Hibernate
  • Spring-boot-starter-cache: Enables the Spring framework to support caching
  • Spring-boot-starter-data-rest: Use Spring Data REST to expose a simple REST service

To learn more about the Starter project, read: Initializing Projects with Spring Boot Starters – Web and JPA

Spring Boot Starter Parent

Almost all Spring Boot projects use spring-boot-starter-parent as a parent configuration in the POM.xml configuration:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>. 2.0.0 RELEASE</version>
</parent>
Copy the code

Parent POMs allows us to better manage multiple sub-projects or modules:

  • Configuration related: Java version and other properties
  • Dependency management: Version of a dependency
  • Default plug-in configuration

For a more in-depth understanding of Spring Boot Starter Parent, please read: Introduction to Spring Boot Starter Parent

Spring Initializr: Make creating a Spring Boot project as fast as driving an F1 car

Spring Initializr is a great tool for initializing a Spring Boot project.

It provides a very simple UI through which we can create different types of Spring projects, such as:

  • The Web application
  • RESTful services
  • Batch applications

Let’s see how to create a project using Spring Initializr:

Just a few simple steps, as shown above:

  • Open Spring Initializr (start.spring.io/)
    • The Group inputcom.in28minutes.springboot
    • An Artifact typestudent-services
    • The dependency to chooseWeb
  • Click at the bottom of the pageGenerate Projectbutton
  • Import the project tointelliJorEclipseCan.

For a more in-depth understanding of Spring Initializr, read: Spring Initializr – Bootstrap Your Spring Boot Applications at F1 Speed!

Spring Boot and embedded servers

Once we have created a deployable package, we can embed a server (such as Tomcat) in the deployment package.

For example, for a Spring Boot application, we can build a JAR package that contains an Embedded Tomcat server, which we can then start as a normal Java program.

An embedded server means that we include server binaries (such as tomcat.jar) in our deployment package

Let’s look at the spring-boot-starter-web dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>2.0.0. M6</version>
    <scope>compile</scope>
</dependency>
Copy the code

As you can see, Starter Web already includes Starter Tomcat dependencies by default:

  • Tomcat is the default embedded server for Spring Boot
  • Spring Boot also supports Jetty and Undertow

For more insight into Embedded Servers, read: Spring Boot and Embedded Servers-Tomcat, Jetty, and Undertow

Spring Data

Official show projects. Spring. IO/spring – the data… :

Spring Data provides a simple, unified Data access layer based on the Spring programming model while retaining some of the features of the underlying Data store. It makes it easy to use data access technologies, relational and non-relational databases, map-Reduce frameworks, and cloud-based services.

In short, Spring Data provides a unified abstraction that allows us to use a unified access layer regardless of the underlying diversity of Data sources.

Spring Data includes sub-modules such as:

  • Spring Data JPA – Relational database
  • Spring Data MongoDB
  • Spring Data REST – Encapsulates the Spring Data repository and provides an excellent REST API service

To learn more about Spring Data, read: Introduction to Spring Data JPA, Spring Data REST, and MongoDB

Other references

  • Spring MVC:www.youtube.com/watch?v=BjN…
  • Spring Boot:www.youtube.com/watch?v=PSP…
  • Eclipse:www.youtube.com/watch?v=s4S…
  • Maven:www.youtube.com/watch?v=0CF…
  • JUnit:www.youtube.com/watch?v=o5k…
  • Mockito:www.youtube.com/watch?v=d2K…