Component Layer
📝 Note: This guide uses generic placeholder names to be reusable across any Spring Boot microservice. Replace with your actual implementation:
{YourService}→ Your service name (e.g.,OrderService,PaymentService)BusinessService→ Your core service (e.g.,OrderService,UserService)DataService→ Your data processing service (e.g.,PaymentService,InventoryService)IntegrationService→ Your external integration (e.g.,PaymentGatewayService){RequestType}→ Your request DTO (e.g.,CreateOrderRequest){ResponseType}→ Your response DTO (e.g.,OrderResponse)
Overview
Spring @Component beans that integrate with Spring Boot framework features for type conversion and health monitoring.
Package: com.example.microserviceorch.component.example
Component Types
1. Type Converters
Automatic conversion of HTTP parameters to domain types via Spring's ConversionService.
Key Interface: Converter<String, EnumType>
2. Health Indicators
Custom health checks that integrate with Spring Boot Actuator for monitoring.
Key Interface: HealthIndicator
Quick Examples
Type Converter
@Component
public class EnumConverter implements Converter<String, EnumType> {
@Override
public EnumType convert(String value) {
return EnumType.fromValue(value);
}
}
Health Indicator
@Component
public class ServiceHealthIndicator implements HealthIndicator {
@Autowired
private Environment environment;
@Autowired
private BuildProperties buildProperties;
@Override
public Health health() {
return Health.up()
.withDetail("environment", String.join(",", environment.getActiveProfiles()))
.withDetail("version", buildProperties.getVersion())
.build();
}
}
Core Principles
- Stateless or Thread-Safe: Components are singletons
- Pure Conversion: No business logic in converters
- Lightweight Health Checks: Fast metadata retrieval only
- Dependency Injection: Constructor injection preferred
- Automatic Registration: Via component scanning
Documentation
- Type Converters Guide - Converter implementation and patterns
- Health Indicators Guide - Health check integration
- Testing Guide - Unit and integration tests
Examples
See /examples/ for:
EnumConverter.java- String-to-enum converterEnumType.java- Enum with JSON supportServiceHealthIndicator.java- Service metadata health checkEnumConverterTest.java- Converter unit testsServiceHealthIndicatorTest.java- Health indicator unit tests
Design Patterns
- Strategy Pattern: Different converters for different types
- Template Method: HealthIndicator interface
- Singleton Pattern: Component scope
- Adapter Pattern: Type conversion
Anti-Patterns
❌ Business logic in converters
❌ Expensive operations in health checks
❌ Stateful components without thread safety
❌ Manual dependency creation
Code Formatting
All code uses Spotless with Google Java Format (AOSP):
./gradlew spotlessApply