Skip to content

Spring Boot - basics

Spring - disadvantages

An application based on the Spring framework consists of many elements. In order to use the framework's capabilities, we need to add some jars to the project dependency, for example:

  • spring-web
  • spring-webmvc
  • spring-core
  • spring-context
  • spring-aop
  • jakarta.annotation-api
  • tomcat-embed-core

These dependencies are not only from the org.springframework group. Hence, configuring an application that is to use the Spring framework is problematic because we must have the knowledge of:

  • which dependencies from the org.springframework group to use
  • what other dependencies to use in our project to make it run correctly
  • what versions of these dependencies to use

Moreover, the combination of all these dependencies (ie their configuration) is non-trivial. When making a mistake (such as using dependencies in an incompatible version) the messages may not be detailed enough.

The Spring Boot project

There are dozens of smaller projects around the Spring framework, however the most important and most used is Spring Boot. In modern applications where the technological choice is Spring, it is practically always used. Its main goal is to eliminate the disadvantages described above, i.e. by using Spring Boot:

  • we don't have to remember about specific libraries that the project uses
  • we don't need to know the version of compatible libraries
  • we get a default configuration that can be easily adapted to your needs
  • we don't need to use the war files to run the applications
  • we do not have to use the xml files to configure the project (which is often necessary when not using SB)

Starters

While creating applications and considering which technology stack we should choose, we most often combine a given technology with a given application layer. The creators of Spring and its community are preparing special projects that contain sets of needed libraries, their automatic configuration. These projects can be easily used in the application as normal dependencies. We call these projects starters. Each starter can consist of many dependencies and even different starters.

NOTE: We can also create our own starters.

NOTE: Since the starter may have integration with an external application enabled by default, just adding the launchpad to the project's dependencies may cause it to stop starting correctly.

Create projects

If we want to create a basic project based on Spring Boot, we can use:

  • website start.spring.io
  • IntelliJ IDEA Ultimate (using File->New->Project->Spring Initializr)

In both cases, make the following decisions:

  • whether the project should be based on Mavena orGradle
  • what version of Spring Boot we want to use (most often selected by default, i.e. the last stable one, i.e. one that is not in the SNAPSHOT version)
  • provide basic data about the project (name of the artifact, etc.)
  • choose the best Java version
  • choose the starters that we want to use in the project
  • choose dependencies that are not starters and are needed in the project

NOTE: In this section, all examples will be based on a project using Maven.

NOTE: Official starters most often point to the org.springframework.boot group, and their name follows the convention spring-boot-starter- {project_name}.

Sample base project

In this knowledge base, some of the functionality of the following primers will be discussed:

  • spring-boot-starter-web
  • spring-boot-starter-data-jpa
  • spring-boot-starter-thymeleaf
  • spring-boot-starter-security

pom.xml file

The following pom.xml file was generated from start.spring.io after selecting dependenciessuch as:

  • Spring Web
  • Spring Data JPA
  • Thymeleaf
  • H2 Database
  • Lombok
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version> <!-- A newer version can be available -->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>pl.sdacademy</groupId>
    <artifactId>sb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sb</name>
    <description>sdacademy demo project</description>

    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

pom.xml file content

After selecting the appropriate dependencies and generating the project, in the dependencies section we will find the selected starters (spring-boot-starter-web, spring-boot-starter-data-jpa,spring-boot-starter-thymeleaf) and dependent libraries (lombok,h2).

In the pom.xml file we will not find information about specific versions of these dependencies directly. This does not mean that these versions are arbitrary or unknown. The generated project, in the parent section, contains a reference to the so-called parent pom (sprint-boot-starter-parent-VERSION.pom), and this one points to parent pomspring-boot-dependencies, which in the properties section contains information about compatible dependency versions.

In the pom.xml file you will also findspring-boot-starter-test, which is used to test the application. In dependencies (which cannot be seen directly) there is also [JUnit5] (../ testing_oprogramowania_podstawki / junit / introducing.md), [AssertJ] (../software_testing_basics/assertj/assertj.md) and [Mockito] (../software_testing_advanced/mockito.md).

NOTE: In the project discussed above, the spring-boot-starter-security starter was omitted. It will be discussed in another section.

Running the project

Depending on the [starters] (# starters) used, the application may launch in different ways. In our projects, however, we very often use the spring-boot-starter-web orspring-boot-starter-webflux starters (outside the scope of this training), which run the application directly on, for example, Tomcat. If you are using IntelliJ Ultimate, the default configuration should be available after importing the generated project into the IDE. After starting the application, we should see a message in the logs confirming its correct launch:

INFO 9372 --- [main] pl.sdacademy.sb.SbApplication : Started SbApplication in 2.448 seconds (JVM running for 2.996)

If you see the following error during startup:

Web server failed to start. Port 8080 was already in use.

in the application.properties file (which is discussed [here] (application_configuration.md # applicationproperties-file)) add the lineserver.port = 8081.

NOTE: If you do not have access to IntelliJ Ultimate, you can also run the application using Maven and the command line: mvn spring-boot: run.