是否可以将spring boot @Value与javax.validation.constraints结合使用? [英] Is it possible to combine spring boot @Value with javax.validation.constraints?

查看:237
本文介绍了是否可以将spring boot @Value与javax.validation.constraints结合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to validate values in application.properties. I use @Value to inject and @NotBlank to validate the method parameter in a class annotated with @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.

I have included the hibernate-validator and the hibernate-validator-annotation-processor properties. Using Spring 2.2.0.RELEASE.

它不起作用.该值已注入,但未经验证.我想念什么?

It doesn't work. The value is injected, but not validated. What am I missing?

推荐答案

添加

Add @Validated to your configuration class.

@Configuration
@Validated
public class MyConfig {

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

        ...
    }

}

这篇关于是否可以将spring boot @Value与javax.validation.constraints结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆