且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Spring @Value时如何进行简单的属性验证

更新时间:2023-11-24 23:07:28

您可以将组件用作属性占位符本身.然后,您可以使用所需的任何验证.

You can use the component as a property placeholder itself. And then you may use any validation that you want.

@Component
@Validated
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "my")
public class MyService {

    @NotBlank
    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    ...
}

您的 my.properties 文件将如下所示:

my.username=felipe