MethodValidationPostProcessor导致进一步的验证失败

时间:2017-12-13 13:19:35

标签: java spring spring-boot spring-cloud spring-cloud-consul

我正在尝试将Spring Cloud Consul添加到现有的Spring Boot应用程序中并启用弹簧验证。在我运行应用程序之前一切都很好,并且在使用MethodValidationPostProcessor注释的属性bean验证失败

据我所知,这是发生的事情:

  1. CustomBeanValidationPostProcessor为属性bean
  2. 创建代理
  3. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'heartbeatProperties' defined in issues.validation.Application: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Bean state is invalid: ttlUnit - may not be null; ttlValue - must be greater than or equal to 1; intervalRatio - must be greater than or equal to 0.1 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE] at issues.validation.Application.main(Application.java:25) [classes/:na] Caused by: org.springframework.beans.factory.BeanInitializationException: Bean state is invalid: ttlUnit - may not be null; ttlValue - must be greater than or equal to 1; intervalRatio - must be greater than or equal to 0.1 at org.springframework.validation.beanvalidation.BeanValidationPostProcessor.doValidate(BeanValidationPostProcessor.java:116) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.validation.beanvalidation.BeanValidationPostProcessor.postProcessAfterInitialization(BeanValidationPostProcessor.java:94) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] ... 15 common frames omitted 启动并尝试验证代理字段,但失败并显示以下内容:

    MethodValidationPostProcessor
  4. 以下是重现此问题的最低限度项目:https://github.com/bashnesnos/cas-spring-cloud-consul-issue

    问题:

    1. 如果有人知道任何解决办法,以避免特定bean的SELECT * FROM table WHERE (abs(val)*100000)%10 <> 0
    2. P.S。我也为Spring创建了一张票https://github.com/spring-projects/spring-boot/issues/11249,但我想在社区中要求解决方案可能会更容易。

1 个答案:

答案 0 :(得分:1)

您的CustomBeanValidationProcessor可以实现Ordered。然后您可以更改顺序,以便之前调用它。

public class CustomBeanValidationPostProcessor extends 
    BeanValidationPostProcessor implements Ordered {

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE; // or another value for a proper order position
    }
}