具有自定义分配的 RequiredArgsConstructor

RequiredArgsConstructor with custom assignment

我很好奇是否可以在构造函数调用期间使用@RequiredArgsConstructor 分配自定义值。即

private String value1;
private String value2;
private String value3;

public MyConstructor(String value1, String value2) {
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = createString();
}

相反,我会寻找:

@RequiredArgsConstructor
public class MyConstructor {
    @NonNull
    private String value1;
    @NonNull
    private String value2;
    private String value3;

    private String createString() {
        return "test";
    }
}

可以直接在属性定义中赋值createString()。因此,当创建新实例时,也会初始化 value3

@RequiredArgsConstructor
public class MyConstructor {
    @NonNull
    private String value1;
    @NonNull
    private String value2;
    private String value3 = createString();

    private String createString() {
        return "test";
    }
}