
Play Store Application link β Spring Boot in 13 steps – App on Google Play
Spring Boot makes application development faster and easier by abstracting complex configurations. One of its key features is Spring Boot Starters, which simplify dependency management and reduce boilerplate code. In this post, we’ll explore the concept of starters, their role in Spring Boot, and how they complement auto-configuration.
Github project link – https://github.com/kuldeep101990/Spring_Boot_Starters
Step 1: Setting Up a Spring Boot Project
Using Spring Initializr
- Go to Spring Initializr.
- Fill in the details:
- Project: Maven
- Language: Java
- Spring Boot Version: 3.x.x
- Dependencies: Spring Web, Spring Data JPA, H2 Database
- Click Generate, and extract the downloaded ZIP file.
Import into IDE
- Open your IDE (e.g., IntelliJ or Eclipse).
- Import the extracted project as a Maven project.
Step 2: Understanding Spring Boot Starters
Spring Boot starters are pre-configured dependency bundles that simplify adding libraries to your project. Instead of manually managing multiple dependencies, you can include a single starter, and Spring Boot takes care of the rest.
Examples of Common Starters:
- spring-boot-starter-web: Provides dependencies for web applications, including Spring MVC and an embedded Tomcat server.
- spring-boot-starter-data-jpa: Adds dependencies for JPA and Hibernate.
- spring-boot-starter-security: Includes Spring Security for authentication and authorization.
Step 3: Using Spring Boot Starters in a Project
Example: REST API with spring-boot-starter-web
The spring-boot-starter-web
includes everything needed to create REST APIs.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring Boot Starter Web!";
}
}
Run the application and access http://localhost:8080/hello
to see:
Hello from Spring Boot Starter Web!
Example: JPA with spring-boot-starter-data-jpa
The spring-boot-starter-data-jpa
bundles everything needed to work with JPA and Hibernate.
Configuration:
Add the following to your application.properties
:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
Entity and Repository:
package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Entity
class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
interface UserRepository extends JpaRepository<User, Long> {}
@RestController
class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/users")
public Iterable<User> getUsers() {
return userRepository.findAll();
}
}
Run the application and access http://localhost:8080/users
to fetch data from the H2 database.
Step 4: How Starters and Auto-Configuration Work Together
Starters and auto-configuration complement each other:
- Starters add required dependencies to your project.
- Auto-Configuration sets up default configurations for those dependencies.
For example:
- Adding
spring-boot-starter-web
includes dependencies for Spring MVC and Tomcat. - Spring Bootβs auto-configuration detects these and sets up a default web environment.
Customizing Behavior:
You can customize starter behavior by excluding specific dependencies or overriding default configurations in application.properties
.
Step 5: Extending Starters
You can create custom starters to standardize dependencies across projects.
Example: Adding Jackson for JSON Processing
Jackson is included in spring-boot-starter-web
for JSON serialization/deserialization.
@RestController
class ProductController {
@GetMapping("/product")
public Product getProduct() {
return new Product(1, "Laptop");
}
}
class Product {
private int id;
private String name;
public Product(int id, String name) {
this.id = id;
this.name = name;
}
// Getters and Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Access http://localhost:8080/product
to see the JSON response:
{
"id": 1,
"name": "Laptop"
}
Step 6: Comparing Starters and Manual Dependency Management
Before starters:
- Developers manually included individual dependencies in
pom.xml
. - Risk of version mismatches and missing transitive dependencies.
With starters:
- Starters bundle related dependencies with compatible versions.
- Reduce boilerplate and simplify configuration.
Conclusion
In this guide, we:
- Explored what Spring Boot starters are and how they simplify dependency management.
- Used
spring-boot-starter-web
andspring-boot-starter-data-jpa
to build a REST API and integrate with a database. - Discussed how starters work with auto-configuration to create seamless applications.
By leveraging starters, you can focus on application logic while Spring Boot handles the heavy lifting of dependency management and configuration.