Spring Boot Starter Dependencies

Spring Boot Starter Dependencies

Spring Boot starters are a set of convenient dependency descriptors that you can include in your application. They are a way to bundle common dependencies together to simplify your build configuration.

Purpose:

 To reduce the boilerplate code and configuration required to set up Spring applications by providing a curated set of dependencies for various functionalities.

Common Spring Boot Starters

1. spring-boot-starter-web:

  • Description: For building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • Dependencies: Spring MVC, Embedded Tomcat, Jackson (for JSON processing).

2. spring-boot-starter-data-jpa:

  • Description: For building JPA-based repositories. Includes Hibernate as the default JPA implementation.
  • Dependencies: Spring Data JPA, Hibernate, Spring ORM.

3. spring-boot-starter-security:

  • Description: For adding Spring Security to an application.
  • Dependencies: Spring Security.

4. spring-boot-starter-thymeleaf:

  • Description: For building web applications with Thymeleaf as the view layer.
  • Dependencies: Thymeleaf, Spring MVC.

5. spring-boot-starter-actuator:

  • Description: For adding production-ready features like monitoring and health checks to an application.
  • Dependencies: Actuator.

6. spring-boot-starter-test:

  • Description: For adding common testing dependencies with JUnit, Hamcrest, and Mockito.
  • Dependencies: JUnit, Hamcrest, Mockito.

Spring Boot Starter Dependencies

spring-boot-starter-web
Example
1. spring-boot-starter-web

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

MainApplication.java
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
```

HelloController.java
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
```

In this example:

  • Starter Dependency: spring-boot-starter-web includes everything needed to create a web application, including an embedded Tomcat server.
  • Controller: A simple REST controller to handle HTTP GET requests.

spring-boot-starter-data-jpa
2. spring-boot-starter-data-jpa

pom.xml
```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
```

application.properties
```properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
```

Entity Class
```java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    // Getters and Setters
}
```

Repository Interface
```java
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Long> {
}
```

Service Class
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public Iterable<User> findAll() {
        return userRepository.findAll();
    }
}
```

Controller Class
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Iterable<User> getAllUsers() {
        return userService.findAll();
    }
}
```

In this example:

  • Starter Dependency: spring-boot-starter-data-jpa includes everything needed to use Spring Data JPA with Hibernate as the JPA provider.
  • Entity, Repository, Service, and Controller: Basic CRUD operations using JPA.

Summary of Spring Boot Starter Dependencies

  • Spring Boot Starters: Simplify dependency management and application setup by bundling common dependencies.
  • Common Starters: Include `spring-boot-starter-web`, `spring-boot-starter-data-jpa`, `spring-boot-starter-security`, `spring-boot-starter-thymeleaf`, `spring-boot-starter-actuator`, and `spring-boot-starter-test`.

Homepage

Readmore