是否可以将spring boot @Value与javax.validation.constraints结合使用? [英] Is it possible to combine spring boot @Value with javax.validation.constraints?
本文介绍了是否可以将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-validator
和hibernate-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屋!
查看全文