Constants 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)
Purpose
The constants layer provides centralized, immutable constants and type-safe enumerations used throughout the application. It ensures consistency, prevents magic strings/numbers, and provides compile-time validation.
Core Principles
- Immutability: All constants are final and cannot be modified
- Type Safety: Enums over strings for finite sets of values
- Centralization: Single source of truth for application-wide values
- Semantic Naming: Clear, descriptive names following Java conventions
- Static Import Friendly: Designed for clean static imports
Package Structure
constant/
├── Constants.java # Application-wide constants
└── NewRelicErrorMessages.java # Expected error messages enum
Documentation
📚 Guides
Comprehensive guides covering constant layer development:
- Design Patterns - Constant Interface, Type-Safe Enum, Static Import patterns
- Best Practices - Immutability, naming conventions, domain grouping
- Testing Strategies - Unit tests, immutability tests, enum tests
- Anti-Patterns - Common mistakes and how to avoid them
💡 Examples
Compilable Java examples demonstrating key concepts:
- ConstantsExample.java - Immutable constant class pattern
- NewRelicErrorMessagesExample.java - Type-safe enum with values
- StaticImportUsageExample.java - Static import best practices
- CollectionConstantsExample.java - Immutable collection constants
Quick Reference
Key Patterns
- Constant Class: Final class with
public static final fields and private constructor
- Type-Safe Enum: Enum with encapsulated values and getter methods
- Static Import: Import constants directly for cleaner code
- Immutable Collections: Use
List.of() and Set.of() for collection constants
Naming Conventions
- Constants:
UPPER_SNAKE_CASE (e.g., BEARER_TOKEN_PREFIX)
- Enum Class:
PascalCase (e.g., ExpectedErrorMessages)
- Enum Values:
UPPER_SNAKE_CASE (e.g., INCORRECT_EMAIL_OR_PASSWORD)
- Enum Methods:
camelCase (e.g., getErrorMessage())
Usage Example
import static com.example.microserviceorch.constant.Constants.*;
public class BusinessService {
public String processToken(String authorization) {
if (authorization.startsWith(BEARER_TOKEN_PREFIX)) {
return authorization.substring(BEARER_TOKEN_PREFIX.length());
}
return authorization;
}
}
Code Formatting
All Java code is formatted using Spotless with Google Java Format (AOSP style):
./gradlew spotlessApply
Related Documentation
For detailed guidance, refer to the guides above. For working examples, see the examples directory.
1---2name: constant3description: Constants Layer4---5# Constants Layer67> **📝 Note:** This guide uses generic placeholder names to be reusable across any Spring Boot microservice.8> Replace with your actual implementation:9> - `{YourService}` → Your service name (e.g., `OrderService`, `PaymentService`)10> - `BusinessService` → Your core service (e.g., `OrderService`, `UserService`)11> - `DataService` → Your data processing service (e.g., `PaymentService`, `InventoryService`)12> - `IntegrationService` → Your external integration (e.g., `PaymentGatewayService`)13> - `{RequestType}` → Your request DTO (e.g., `CreateOrderRequest`)14> - `{ResponseType}` → Your response DTO (e.g., `OrderResponse`)151617## Purpose1819The constants layer provides centralized, immutable constants and type-safe enumerations used throughout the application. It ensures consistency, prevents magic strings/numbers, and provides compile-time validation.2021## Core Principles2223- **Immutability**: All constants are final and cannot be modified24- **Type Safety**: Enums over strings for finite sets of values25- **Centralization**: Single source of truth for application-wide values26- **Semantic Naming**: Clear, descriptive names following Java conventions27- **Static Import Friendly**: Designed for clean static imports2829## Package Structure3031```32constant/33├── Constants.java # Application-wide constants34└── NewRelicErrorMessages.java # Expected error messages enum35```3637## Documentation3839### 📚 Guides4041Comprehensive guides covering constant layer development:4243- **[Design Patterns](guides/patterns.md)** - Constant Interface, Type-Safe Enum, Static Import patterns44- **[Best Practices](guides/best-practices.md)** - Immutability, naming conventions, domain grouping45- **[Testing Strategies](guides/testing.md)** - Unit tests, immutability tests, enum tests46- **[Anti-Patterns](guides/anti-patterns.md)** - Common mistakes and how to avoid them4748### 💡 Examples4950Compilable Java examples demonstrating key concepts:5152- **[ConstantsExample.java](examples/ConstantsExample.java)** - Immutable constant class pattern53- **[NewRelicErrorMessagesExample.java](examples/NewRelicErrorMessagesExample.java)** - Type-safe enum with values54- **[StaticImportUsageExample.java](examples/StaticImportUsageExample.java)** - Static import best practices55- **[CollectionConstantsExample.java](examples/CollectionConstantsExample.java)** - Immutable collection constants5657## Quick Reference5859### Key Patterns6061- **Constant Class**: Final class with `public static final` fields and private constructor62- **Type-Safe Enum**: Enum with encapsulated values and getter methods63- **Static Import**: Import constants directly for cleaner code64- **Immutable Collections**: Use `List.of()` and `Set.of()` for collection constants6566### Naming Conventions6768- **Constants**: `UPPER_SNAKE_CASE` (e.g., `BEARER_TOKEN_PREFIX`)69- **Enum Class**: `PascalCase` (e.g., `ExpectedErrorMessages`)70- **Enum Values**: `UPPER_SNAKE_CASE` (e.g., `INCORRECT_EMAIL_OR_PASSWORD`)71- **Enum Methods**: `camelCase` (e.g., `getErrorMessage()`)7273### Usage Example7475```java76import static com.example.microserviceorch.constant.Constants.*;7778public class BusinessService {79 public String processToken(String authorization) {80 if (authorization.startsWith(BEARER_TOKEN_PREFIX)) {81 return authorization.substring(BEARER_TOKEN_PREFIX.length());82 }83 return authorization;84 }85}86```8788## Code Formatting8990All Java code is formatted using **Spotless** with Google Java Format (AOSP style):9192```bash93./gradlew spotlessApply94```9596## Related Documentation9798- [Architecture Overview](../../ARCHITECTURE.md) - Overall application architecture99- [Model Layer](../model/skill.md) - Data transfer objects and domain models100- [Service Layer](../service/skill.md) - Business logic and service orchestration101102---103104For detailed guidance, refer to the guides above. For working examples, see the examples directory.