1

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

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

  1. MethodValidationPostProcessor为属性 bean 创建一个代理
  2. CustomBeanValidationPostProcessor启动并尝试验证代理字段并失败并显示以下内容:

    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
    

这是重现该问题的最小项目:https ://github.com/bashnesnos/cas-spring-cloud-consul-issue

问题:

  1. 如果有人知道任何解决方法可以避免MethodValidationPostProcessor使用特定的 bean?

PS 我也为 Spring 创建了一张票https://github.com/spring-projects/spring-boot/issues/11249,但我想在社区中寻求解决方法可能更容易。

4

1 回答 1

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
    }
}
于 2017-12-13T14:30:11.190 回答