我们可以在不更改源代码的情况下编辑 Spring 验证注释吗?

Can we edit Spring validation annotation without changing the source code?

public class Foo {

    @NotNull
    @Pattern(regexp = "[0-9]+"
    private String number;

}

生产现场有支持负数的要求。有没有一种方法可以在不更改源 class 的情况下更改正则表达式值或禁用验证?要添加的任何配置 xml?

我知道我们可以编辑正则表达式,但正在寻找无需编辑源文件的选项。

Spring 使用正则表达式验证,因此您可以像这样简单地调整正则表达式模式:

public class Foo {
    @NotNull
    @Pattern(regexp = "-?[0-9]+")
    private String number;
}

但是,如果您的意思是对模式进行参数化,那么您可以像这样使用 属性:

public class Foo {
    @NotNull
    @Pattern(regexp = "${regex.pattern}")
    private String number;
}

并在 application.properties 中添加 属性

regex.pattern=-?[0-9]+