是否可以将 spring boot @Value 与 javax.validation.constraints 结合使用?

Is it possible to combine spring boot @Value with javax.validation.constraints?

我想验证 application.properties 中的值。 我使用@Value 注入并使用@NotBlank 验证class 中的方法参数,并用@Configuration 注释。

createSslContext(
            @Value("#{systemProperties['javax.net.ssl.trustStore']}") 
            @NotBlank(message = "Truststore is needed") 
            String path)

我添加了 hibernate-validatorhibernate-validator-annotation-processor 属性。使用 Spring 2.2.0.RELEASE.

没用。该值已注入,但未验证。我错过了什么?

@Validated 添加到您的配置 class。

@Configuration
@Validated
public class MyConfig {

    @Bean
    MyClass createSslContext(
        @Value("#{systemProperties['javax.net.ssl.trustStore']}")
        @NotBlank(message = "Truststore is needed") final
        String path) {

        ...
    }

}