Skip to content

The ValueChecker module is a custom validation framework that uses AOP to provide flexible, configurable parameter validation for Spring methods. This provides a flexible, reusable validation system that separates validation logic from business logic and supports complex validation scenarios with thread-local state management.

License

Notifications You must be signed in to change notification settings

GOODDAYDAY/value-checker-java-17

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Value Checker Java 17

A flexible Java 17 validation framework using Spring AOP that allows custom validation logic through configurable handler classes.

Overview

Value Checker is an advanced validation library that extends beyond simple parameter checks. It provides a flexible framework where you can define custom validation handlers and chain multiple validation checks together using Spring Expression Language (SpEL) for parameter extraction. This is the Java 17 version with updated dependencies and modern Java features.

Features

  • Custom validation handlers: Define your own validation logic by implementing IValueCheckerHandler
  • SpEL integration: Use Spring Expression Language to extract and pass parameters to validators
  • Multiple validation chains: Apply multiple validators to a single method
  • Thread-safe context: ValueCheckerReentrantThreadLocal for maintaining validation state
  • Method reflection caching: Efficient method lookup and caching for performance
  • Spring Boot 3.x integration: Seamless integration with latest Spring Boot applications
  • Java 17 performance: Leverages modern JVM optimizations and language features

Dependencies

  • Java 17
  • Spring Boot 3.5.5 (AOP)
  • Apache Commons Lang3 & Collections
  • Lombok

Quick Start

1. Add the dependency to your Maven project:

<dependency>
  <groupId>com.goody.utils</groupId>
  <artifactId>value-checker-java-17</artifactId>
  <version>1.0.0</version>
</dependency>

2. Enable Spring Boot AOP in your application:

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

3. Create a custom validation handler:

@Service
public class UserValidationHandler implements IValueCheckerHandler {

    public void validateUser(Long userId, String username) {
        if (userId == null || userId <= 0) {
            throw new ValueIllegalException("Invalid user ID");
        }
        if (username == null || username.trim().isEmpty()) {
            throw new ValueIllegalException("Invalid username");
        }
        // Custom validation logic here
    }

    public void validateAge(Integer age) {
        if (age == null || age < 0 || age > 150) {
            throw new ValueIllegalException("Invalid age");
        }
    }
}

4. Use the validation in your service:

@Service
public class UserService {

    @ValueCheckers(checkers = {
            @ValueCheckers.ValueChecker(
                    method = "validateUser",
                    keys = {"#userId", "#username"},
                    handler = UserValidationHandler.class
            ),
            @ValueCheckers.ValueChecker(
                    method = "validateAge",
                    keys = "#user.age",
                    handler = UserValidationHandler.class
            )
    })
    public void createUser(Long userId, String username, User user) {
        // Method implementation
        // Validation happens automatically before execution
    }
}

Core Components

@ValueCheckers Annotation

The main annotation that enables validation for a method:

@ValueCheckers(checkers = {
        @ValueCheckers.ValueChecker(
                method = "validationMethodName",    // Method name in handler
                keys = {"#param1", "#param2"},      // SpEL expressions for parameters
                handler = YourHandlerClass.class    // Handler class
        )
})

ValueChecker Configuration

Each @ValueChecker supports:

  • handler: Class implementing IValueCheckerHandler
  • method: Method name to invoke (default: "verify")
  • keys: String array of SpEL expressions to extract parameters

IValueCheckerHandler Interface

Marker interface for validation handlers. All validation handlers must implement this interface:

public interface IValueCheckerHandler {
    // Your validation methods
}

SpEL Expression Support

Extract parameters using Spring Expression Language:

  • "#paramName" - Direct parameter
  • "#object.property" - Object property
  • "#object.method()" - Method call result
  • {"#param1", "#param2"} - Multiple parameters

Usage Examples

Basic Parameter Validation

@ValueCheckers(checkers = {
        @ValueCheckers.ValueChecker(
                method = "validateId",
                keys = "#id",
                handler = ValidationHandler.class
        )
})
public void processUser(Long id, String name) {
    // Validation happens before method execution
}

Object Property Validation

@ValueCheckers(checkers = {
        @ValueCheckers.ValueChecker(
                method = "validateUserData",
                keys = {"#user.id", "#user.name"},
                handler = UserValidationHandler.class
        )
})
public void updateUser(User user) {
    // Validates user.getId() and user.getName()
}

Multiple Validation Chains

@ValueCheckers(checkers = {
        @ValueCheckers.ValueChecker(method = "validateId", keys = "#id", handler = IdValidator.class),
        @ValueCheckers.ValueChecker(method = "validateName", keys = "#name", handler = NameValidator.class),
        @ValueCheckers.ValueChecker(method = "validateBusiness", keys = {"#id", "#name"}, handler = BusinessValidator.class)
})
public void complexValidation(Long id, String name) {
    // Multiple validators run in sequence
}

Thread-Safe Context Usage

@Service
public class ContextAwareValidator implements IValueCheckerHandler {

    public void validateWithContext(String value) {
        // Store value in thread-local context
        ValueCheckerReentrantThreadLocal.getOrDefault(String.class, value);

        // Use context in validation logic
        String contextValue = ValueCheckerReentrantThreadLocal.getOrDefault(String.class, "");
        if (!contextValue.equals(value)) {
            throw new ValueIllegalException("Context validation failed");
        }
    }
}

Exception Handling

All validation failures should throw ValueIllegalException:

public void validateUser(String username) {
    if (username == null || username.trim().isEmpty()) {
        throw new ValueIllegalException("Username cannot be empty");
    }
}

Performance Features

  • Method caching: Reflection results are cached for improved performance
  • Bean lookup optimization: Handler instances are cached by Spring
  • Thread-local context: Reentrant thread-local storage for complex validations
  • Java 17 optimizations: Benefits from modern JVM performance improvements

Advanced Features

Handler Bean Naming

Handlers are automatically registered with uncapitalized class names:

  • UserValidationHandler → bean name: userValidationHandler
  • OrderValidator → bean name: orderValidator

Reentrant Validation

The framework supports nested validation calls through ValueCheckerReentrantThreadLocal, allowing complex validation scenarios where one validator may call another method with validation.

Java 17 Specific Benefits

This version takes advantage of:

  • Performance improvements in Java 17 JVM
  • Updated Spring Boot 3.x with modern Spring features
  • Enhanced reflection performance for method caching
  • Improved garbage collection for better application performance
  • Modern language features for cleaner, more maintainable code

Building and Testing

# Compile the project
mvn clean compile

# Run tests
mvn test

# Package the jar
mvn package

# Install to local repository
mvn install

Migration from Java 8 Version

If migrating from the Java 8 version:

  1. Update Java version to 17+
  2. Update Spring Boot to 3.x
  3. Verify SpEL expressions work with new Spring version
  4. Update dependency versions as shown in pom.xml
  5. No code changes required - the API remains the same

Best Practices

  1. Keep handlers focused: Each handler should have a single responsibility
  2. Use meaningful method names: Method names should clearly indicate what they validate
  3. Leverage SpEL: Use SpEL expressions to extract exactly what you need
  4. Handle exceptions properly: Always throw ValueIllegalException for validation failures
  5. Consider performance: Complex SpEL expressions may impact performance
  6. Utilize Java 17 features: Take advantage of records, pattern matching, and other modern features in your handlers

License

This project is licensed under the MIT License.

Author

  • Goody - Initial work - Version 1.0, 2022/05/05

About

The ValueChecker module is a custom validation framework that uses AOP to provide flexible, configurable parameter validation for Spring methods. This provides a flexible, reusable validation system that separates validation logic from business logic and supports complex validation scenarios with thread-local state management.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages