Spring环境中的多个@ConfigurationProperties验证器bean [英] Multiple @ConfigurationProperties validator beans in Spring environment
问题描述
使用@ConfigurationProperties
批注将属性注入到bean中时,Spring提供了定义自定义验证器以验证那些属性的功能.
ConfigurationPropertiesBindingPostProcessor
使用固定的Bean名称"configurationPropertiesValidator"
和类org.springframework.validation.Validator
查找此验证器.
现在假设我在模块A中有一个带有验证器的@ConfigurationProperties
.另一个模块B对模块A有依赖性.模块B还定义了自己的@ConfigurationProperties
和自己的验证器.
在应用程序加载时,后处理器仅会拾取这些bean中的一个.这将禁用验证的另一部分.
有解决方案吗?如何在我的应用程序中同时启用两个配置属性验证器?
我刚刚遇到了同样的问题,并且意识到ConfigurationPropertiesBindingPostProcessor
验证是否用@ConfigurationProperties
注释的类本身实现了Validator接口. (请参见org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor#determineValidator
)
因此解决方案是将所有属性验证移至带注释的属性类:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
@ConfigurationProperties("test.properties")
@Component
public class TestProperties implements Validator {
private String myProp;
public String getMyProp()
{
return myProp;
}
public void setMyProp( String myProp )
{
this.myProp = myProp;
}
public boolean supports( Class<?> clazz )
{
return clazz == TestProperties.class;
}
public void validate( Object target, Errors errors )
{
ValidationUtils.rejectIfEmpty( errors, "myProp", "myProp.empty" );
TestProperties properties = (TestProperties) target;
if ( !"validThing".equals( properties.getMyProp() ) ) {
errors.rejectValue( "myProp", "Not a valid thing" );
}
}
}
When using the @ConfigurationProperties
annotation to inject properties into a bean, Spring provides the ability to define a custom validator to validate those properties.
The ConfigurationPropertiesBindingPostProcessor
looks up this validator using the fixed bean name "configurationPropertiesValidator"
and class org.springframework.validation.Validator
.
Now assume I have a @ConfigurationProperties
with its validator in a module A. Another module B has a dependency on module A. Module B also defines its own @ConfigurationProperties
and its own validator.
When the app loads up, the post-processor picks up only one of these beans. This disables the other part of the validation.
Is there a solution to this? How can I keep both configuration property validators enabled in my application?
I just encountered the same problem and realized that ConfigurationPropertiesBindingPostProcessor
verifies if the class annotated with @ConfigurationProperties
implements the Validator interface itself. (see org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor#determineValidator
)
So the solution is to move all property validation to the annotated property class:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
@ConfigurationProperties("test.properties")
@Component
public class TestProperties implements Validator {
private String myProp;
public String getMyProp()
{
return myProp;
}
public void setMyProp( String myProp )
{
this.myProp = myProp;
}
public boolean supports( Class<?> clazz )
{
return clazz == TestProperties.class;
}
public void validate( Object target, Errors errors )
{
ValidationUtils.rejectIfEmpty( errors, "myProp", "myProp.empty" );
TestProperties properties = (TestProperties) target;
if ( !"validThing".equals( properties.getMyProp() ) ) {
errors.rejectValue( "myProp", "Not a valid thing" );
}
}
}
这篇关于Spring环境中的多个@ConfigurationProperties验证器bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!