[Github] 3. Basic-Check: Validation Framework
[Github] 3. Basic-Check: Validation Framework
Introduction
Parameter validation is a common and crucial requirement in daily Java development. Traditional parameter validation typically requires writing extensive if-else conditional code in each method, which is not only redundant and tedious but also prone to omissions. Basic-Check-Java was born to solve this pain point as a lightweight parameter validation framework.
This article will provide an in-depth introduction to Basic-Check-Java’s design philosophy, core features, and practical applications, helping developers quickly master this practical tool.
Github
Design Philosophy and Core Features
Design Philosophy
Basic-Check-Java is designed based on the following core principles:
- Simplicity: Reduce boilerplate code through annotation-driven declarative programming
- Flexibility: Support multiple return strategies to adapt to different business scenarios
- Non-intrusive: Based on AOP implementation with zero intrusion on business code
- Extensibility: Support custom validation rules and processing logic
Core Features
1. Rich Parameter Validation Annotations
Basic-Check-Java provides six commonly used parameter validation annotations:
@CheckNull
: Validates that parameter is not null@CheckString
: Validates that string parameter is not blank@CheckLong
: Validates that Long parameter is greater than -1@CheckCollection
: Validates that collection parameter is not empty@CheckMap
: Validates that Map parameter is not empty@CheckObject
: Validates object parameters using Bean Validation
2. Flexible Return Strategies
Through the returnType
attribute of the @BasicCheck
annotation, three handling strategies are supported when validation fails:
EXCEPTION
(default): Throws IllegalArgumentExceptionEMPTY
: Automatically returns empty values based on method return type (empty collections, empty Map, Optional.empty(), etc.)NULL
: Returns null directly
3. Non-intrusive Implementation Based on Spring AOP
The framework uses AspectJ annotations and Spring AOP technology to perform parameter validation before method execution through aspect-oriented programming, with complete non-intrusion on business code.
In-depth Technical Architecture Analysis
Core Architecture Diagram
|
|
Key Technical Implementation
1. Annotation Design
Using @BasicCheck
as an example, demonstrating elegant annotation design:
|
|
Key design points:
@Target(ElementType.METHOD)
: Can only be applied at method level@Retention(RetentionPolicy.RUNTIME)
: Retain annotation information at runtime- Provides enum-type configuration options ensuring type safety
2. AOP Aspect Implementation
NotNullAndPositiveAspect
is the core component of the framework, handling the main parameter validation logic:
|
|
Implementation highlights:
- Parameter-Annotation Mapping: Obtain method parameters and corresponding annotation information through reflection
- Decoupled Validation Logic: Each validation type is handled independently, facilitating maintenance and extension
- Intelligent Return Handling: Intelligently generate return values based on method return type and configuration strategy
3. Intelligent Return Value Generation
A clever design of the framework is automatically generating appropriate empty values based on method return type:
|
|
This design avoids the complexity of developers manually handling different return types.
Practical Application Examples
Basic Usage Examples
1. Simple Parameter Validation
|
|
2. Collection Parameter Validation
|
|
3. Empty Return Strategy
|
|
Advanced Application Scenarios
1. Complex Object Validation
|
|
2. Mixed Validation Strategies
|
|
Performance Considerations and Best Practices
Performance Analysis
- Reflection Overhead: Framework uses reflection to obtain method parameter information, performance testing recommended for high-concurrency scenarios
- AOP Overhead: Spring AOP is based on proxy pattern with slight performance overhead
- Bean Validation: Object validation has certain performance cost but is generally acceptable
Best Practices
1. Reasonable Validation Strategy Selection
|
|
2. Combined Use of Validation Annotations
|
|
3. Custom Validation Object Design
|
|
Java 8 vs Java 17 Version Differences
Core Difference: Validation API Namespace Change
This is the most important difference between the two versions:
Java 8 version uses traditional javax
namespace:
|
|
Java 17 version uses new jakarta
namespace:
|
|
Complete Dependency Comparison
Java 8 Version Dependencies:
- Spring Boot 2.4.4
- Lombok 1.18.18
javax.validation:validation-api:2.0.1.Final
org.hibernate:hibernate-validator:6.0.1.Final
org.glassfish:javax.el:3.0.1-b09
Java 17 Version Dependencies:
- Spring Boot 3.5.5
- Lombok 1.18.38
org.hibernate.validator:hibernate-validator:8.0.1.Final
org.glassfish:jakarta.el:4.0.2
Extension and Customization
Custom Validation Annotations
The framework adopts an open design, supporting addition of custom validation annotations:
|
|
Custom Return Strategies
ReturnType enum can be extended to add more return strategies:
|
|
Conclusion
Basic-Check-Java is an elegantly designed and practically useful parameter validation framework. It solves the pain points of parameter validation in Java development through the following characteristics:
Core Advantages
- High Development Efficiency: Reduces 80% of parameter validation boilerplate code
- Simple to Use: Only need to add annotations to enjoy complete validation functionality
- Comprehensive Features: Supports multi-level validation of basic types, collections, and complex objects
- Flexible Configuration: Multiple return strategies adapt to different business scenarios
- Non-intrusive: Based on AOP implementation with zero impact on existing code
Applicable Scenarios
- Web Applications: Controller layer parameter validation
- Service Layer: Service method parameter validation
- Utility Classes: Parameter checking for general utility methods
- API Interfaces: Parameter pre-checking before third-party interface calls
Basic-Check-Java embodies excellent framework design principles: simple to use, feature-complete, reliable performance, and easy to extend. It not only significantly improves development efficiency but also helps developers write more robust and elegant code.
Whether for new projects or existing projects, Basic-Check-Java can be quickly integrated and deliver value, making it an indispensable tool in the Java developer’s toolkit.