Spring Boot 3 Expert Skill
This skill provides guidelines and best practices for developing modern Spring Boot 3 applications.
Dependency Injection
Strictly forbid field injection (using @Autowired on fields). You must mandate constructor injection. Use final fields and Lombok's @RequiredArgsConstructor when appropriate to reduce boilerplate code.
Example Context:
@Service
@RequiredArgsConstructor
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentClient paymentClient;
// No @Autowired needed; constructor generated by Lombok
}
Architecture
Enforce a strict layered architecture:
- Controller Layer: Responsible only for HTTP request mapping and delegating to services.
- Service Layer: Contains the core business logic.
- Repository Layer: Handles data access and interactions with the database.
Example Context:
@RestController
@RequestMapping("/api/v1/orders")
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
@PostMapping
public ResponseEntity<OrderResponse> createOrder(@RequestBody @Valid OrderRequest request) {
return ResponseEntity.ok(orderService.processOrder(request));
}
}
Data Modeling
Mandate the use of Java 14+ record types for all DTOs (Data Transfer Objects), Requests, and Responses. This ensures immutability and concise class definitions.
Example Context:
public record OrderRequest(
@NotBlank String customerId,
@Positive BigDecimal amount,
@NotEmpty List<String> itemIds
) {}
Configuration
Emphasize the use of @ConfigurationProperties for managing application configuration over scattered @Value annotations to provide strongly-typed configuration.
Example Context:
@ConfigurationProperties(prefix = "app.payment")
public record PaymentProperties(
@NotBlank String apiUrl,
@Min(1000) int timeoutMs,
int maxRetries
) {}