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'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:
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>
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.
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>
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.
You can always add additional dependencies (beyond the starters) to your POM, just as you would in any other Maven project.
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.
Managing dependencies in Spring Boot applications:
pom.xml
with Maven):<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Other dependencies --> </dependencies>
Dependency management with Maven in Spring Boot:
pom.xml
file and Maven will handle their resolution.pom.xml
):<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Other dependencies --> </dependencies>
Using Gradle for dependency management in Spring Boot:
build.gradle
file when using Gradle.build.gradle
):dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // Other dependencies }
Externalizing dependency versions in Spring Boot:
dependency-versions.properties
):spring.boot.version=2.6.1
Managing transitive dependencies in Spring Boot:
mvn dependency:tree
Customizing dependency resolution in Spring Boot:
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>
Spring Boot BOM (Bill of Materials) for dependency management:
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>
Upgrading dependencies in a Spring Boot project:
mvn versions:display-dependency-updates