Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - Dependency Management

Spring Boot's dependency management is one of its most powerful features, making it easy to ensure consistency and compatibility among the libraries and frameworks in your project. Here's an overview of this topic:

1. Spring Boot Starters

Spring Boot provides a set of "starter" POMs (Project Object Model) to simplify Maven configuration. Each starter is a set of convenient dependency descriptors you can include in your application.

For instance, if you want to create a web application, you can use the spring-boot-starter-web dependency. This starter provides everything you need to build a Spring MVC app and run it as a standalone Java application with an embedded Tomcat server.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. Version Management

One of the key benefits of using Spring Boot's starters is that you don't need to specify versions for any of the included dependencies. Spring Boot manages versions for you and ensures compatibility between different libraries.

This is facilitated by the spring-boot-dependencies POM. When you create a Spring Boot project, you inherit from the spring-boot-starter-parent POM, which in turn inherits from the spring-boot-dependencies POM. This parent POM contains the versions of all the libraries, ensuring compatibility.

3. Overriding a Dependency Version

While Spring Boot manages versions of libraries for you, sometimes you may need to use a different version of a library. In such cases, you can override the version provided by Spring Boot in your project's POM.

For example, to override the version of Hibernate:

<properties>
    <hibernate.version>5.5.0.Final</hibernate.version>
</properties>

4. Excluding Transitive Dependencies

Sometimes, a starter might bring in a dependency that you don't need or want. You can exclude these transitive dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

In this example, the embedded Tomcat server is excluded from the web starter.

5. Additional Dependencies

You can always add additional dependencies (beyond the starters) to your POM, just as you would in any other Maven project.

6. BOMs (Bill Of Materials)

BOMs are a special kind of POM file that can control the versions of a project's dependencies. Spring Boot provides several BOMs, but the most common one is the spring-boot-dependencies BOM.

By using Spring Boot's BOMs, you can import consistent versions of third-party libraries even if you don't use the Spring Boot parent POM.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.5.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

In conclusion, Spring Boot's dependency management system simplifies project configurations, ensures compatibility, and enhances productivity by reducing time spent on troubleshooting version conflicts.

  1. Managing dependencies in Spring Boot applications:

    • Use a build tool like Maven or Gradle to declare and manage project dependencies.
    • Example (pom.xml with Maven):
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
          <!-- Other dependencies -->
      </dependencies>
      
  2. Dependency management with Maven in Spring Boot:

    • Declare dependencies in the pom.xml file and Maven will handle their resolution.
    • Example (pom.xml):
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-jpa</artifactId>
          </dependency>
          <!-- Other dependencies -->
      </dependencies>
      
  3. Using Gradle for dependency management in Spring Boot:

    • Declare dependencies in the build.gradle file when using Gradle.
    • Example (build.gradle):
      dependencies {
          implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
          // Other dependencies
      }
      
  4. Externalizing dependency versions in Spring Boot:

    • Define dependency versions in a central location to manage and update them easily.
    • Example (dependency-versions.properties):
      spring.boot.version=2.6.1
      
  5. Managing transitive dependencies in Spring Boot:

    • Be aware of and manage transitive dependencies brought in by your direct dependencies.
    • Use tools like Maven Dependency Plugin to analyze dependencies.
    • Example (Maven Dependency Plugin):
      mvn dependency:tree
      
  6. Customizing dependency resolution in Spring Boot:

    • Adjust dependency resolution strategies, exclude unwanted dependencies, or use specific versions.
    • Example (pom.xml):
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-dependencies</artifactId>
                  <version>2.6.1</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      
  7. Spring Boot BOM (Bill of Materials) for dependency management:

    • Use Spring Boot's BOM to manage versions of dependencies and ensure compatibility.
    • Example (pom.xml):
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-dependencies</artifactId>
                  <version>2.6.1</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      
  8. Upgrading dependencies in a Spring Boot project:

    • Regularly check for updates and upgrade dependencies to leverage new features and security patches.
    • Example (Maven):
      mvn versions:display-dependency-updates