You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Lombok自动生成的模型类Setter方法中统一调用自定义验证方法?

How to Auto-Inject Validation Logic into Lombok-Generated Setters

Great question! When you're using Lombok's @Data to auto-generate getters and setters but need to run a custom validation method (like Util.validate()) every time a setter is called—without manually writing all those setter methods—here are the two best solutions:

This approach is completely non-intrusive: you don't need to modify your existing @Data model class at all. We'll use AspectJ to intercept all setter calls on your Model class, run the validation logic, and pass the validated value to the original setter.

Step 1: Add AspectJ Dependencies

First, make sure you have AspectJ in your build (Maven example):

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.20.1</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.20.1</version>
</dependency>

Step 2: Create the Validation Aspect

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class ModelValidationAspect {

    // Intercept all setters in Model that take an int parameter
    @Around("execution(void com.yourpackage.Model.set*(int)) && args(inputValue)")
    public void validateSetterInput(ProceedingJoinPoint joinPoint, int inputValue) throws Throwable {
        // Run your custom validation
        int validatedValue = Util.validate(inputValue);
        // Execute the original setter with the validated value
        joinPoint.proceed(new Object[]{validatedValue});
    }
}

Why This Works

  • No code changes to your Model: Keep using @Data as-is.
  • Scalable: Add new int fields to Model later, and their setters will automatically get validation—no extra work needed.
  • Clean separation: Validation logic lives in one place, not scattered across setter methods.

2. Custom Setters + Lombok @Getter (Simple, No Extra Dependencies)

If you only have a small number of fields and don't want to add AspectJ to your project, you can skip @Data and use @Getter to auto-generate getters, then write minimal setter methods that include the validation logic.

Example Code

import lombok.Getter;

@Getter
class Model {
    private int x;
    private int y;

    public void setX(int x) {
        this.x = Util.validate(x);
    }

    public void setY(int y) {
        this.y = Util.validate(y);
    }
}

Pros & Cons

  • Pros: No extra dependencies, code is straightforward and easy to debug.
  • Cons: You have to write each setter manually (though you still get auto-generated getters from Lombok). Not ideal if you have many fields or plan to add more later.

内容的提问来源于stack exchange,提问作者Snehasis Rout

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:22:41