Updated April 10, 2023
Introduction to Spring Boot Dependencies
Dependency management is very easy in Spring boot because of its auto-configuration, it manages every configuration for us, we do not need to worry about the version of the dependency. Dependency is nothing but a library that provides specific support to the application to run and implement. We can manage these dependencies at one place in our application called pom.xml or build.gradle etc. file. It totally depends on which build tool we are using in the application. Every release of the spring boot comes up with new dependencies, also spring boot manages the updating of dependencies if we want to update the spring boot version, that we do not require to manage. In the coming section, we will its internal working usage and implementation in a more detailed way to understand it better for beginners.
Syntax of Spring Boot Dependencies
As we know that spring boot dependency used to provide support to the application, we have a basic syntax for this which can be followed according to the build tool. we are using here one maven pom.xml file. how we can define dependencies there for beginners to understand see below;
<groupId>dependency grou id</groupId>
<artifactId>depenedncy artifactId</artifactId>
<version>version</version>
As you can see in the above syntax we have these main things while defining dependency into pom.xml file, let’s take a look at the practice syntax to get a better idea about it. see below;
Example:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.BUILD-SNAPSHOT</version>
Here the dependency for spring boot starter looks like after all the declaration so now it will download this dependency form maven central for local it will have downloaded into the .m2 folder for us. In the coming section, we will see a different type of dependencies that get automatically added when we create the spring boot project.
How do Dependencies Work in Spring Boot?
As of now we already know that spring boot depends on the dependencies as the name suggest because without it this project itself will be a spring boot application. we have some basic dependencies in place while working with the spring boot application. Also, spring boot is a powerful framework that manages the dependencies internally, we do not need to worry about the version which matches with the spring boot version it will manage by itself. Also whenever the new release of spring boot comes into place it always comes up with new dependencies or adaptations in the existing one.
In this section we will see how and which are the common dependencies are required for spring boot see below;
1) automatically comes: If you can see in the below dependency it is a parent dependency that comes by default when we create the spring boot application from scratch you can verify into the corresponding .xml file. This dependency includes many more other dependencies as the bundle for us.
Code:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.BUILD-SNAPSHOT</version>
</parent>
2) In spring boot we do not require to manage the version for our dependency it will handle by the spring boot application itself.
3) If you want to add or change the version for java then we can use <properties> for that where we can specify the version of java we want to use in our application see below for better understanding;
Code:
<properties>
<java.version>your java version </java.version>
</properties>
4) Let’s take a closer look at the syntax for the build file in spring boot for dependencies see below;
To define plugins inside it:
Code:
plugins {
id 'org.springframework.boot' version '2.2.9.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'org.liquibase.gradle' version '2.0.1'
}
If we want to define plugins inside the spring boot application, then we have to write something like the above. Plugin tag is used to define it; inside it we can define our plugins name.
5) To define the repositories for the dependencies we can use the below format for the build.Gradle file to manage and download our dependencies,
we have to use the repositories tag inside this we can define it should be properties like maven URL, username and password, etc. Take look at the below syntax for better understanding se below;
Code:
repositories {
mavenLocal()
mavenCentral()
maven {
url "your registery url"
credentials {
username "${username}"
password "${password}"
}
}
maven {
url "your registery url"
credentials {
username "${username}"
password "${password}"
}
}
}
6) We can download or add all these dependencies from the maven central repository. IF you want to add any new dependency to your existing spring boot application then just type in the dependency name to the browser and you will get the result,
URL: https://mvnrepository.com/
This is the Url for maven central where you can search any dependency by their name and can choose any version you want which is suitable with your application see below;
7) Now we will see one sample syntax to define all this inside the Gradle file see below;
plugins {
// your plugins will go here ex:
id 'org.springframework.boot' version '2.2.9.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
// etc
}
group = 'your base package name '
sourceCompatibility = 'version of java'
repositories {
mavenLocal()
mavenCentral()
maven {
url "maven url local"
credentials {
username "${usename}"
password "${password}"
}
}
maven {
url "maven url for prod"
credentials {
username "${username}"
password "${password}"
}
}
}
configurations {
// if any
}
dependencies {
// here you can add all your dependencies like below;
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
// your test case goes here ..
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
// you can also add them like below;
compile group: 'org.json', name: 'json', version: '20190722'
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
}
test {
//For junit .
}
Conclusion
Dependency is nothing but a package or bundle which provides support to our application which is needed. All the spring-related annotations and everything’s present inside these dependencies, if any of the dependency is missing or mismatched then we may receive an error while running the application, so it should be very accurate. Else spring boot has made it very easy to use and handle as well for the developers by making so many things automate.
Recommended Articles
This is a guide to Spring Boot Dependencies. Here we discuss the introduction, syntax and how do Dependencies Work in Spring Boot with example and code implementation. You can also go through our other suggested articles to learn more –