Spring Boot 从 1.5.3 升级到 1.5.4,自定义验证配置启动失败

Posted

技术标签:

【中文标题】Spring Boot 从 1.5.3 升级到 1.5.4,自定义验证配置启动失败【英文标题】:Spring Boot upgrade to 1.5.4 from 1.5.3 with custom validation config startup failed 【发布时间】:2017-11-21 20:25:26 【问题描述】:

当 1.5.3 运行良好时。 但是升级到 1.5.4 启动失败...

异常:2017-06-19 22:41:13,400:ERROR org.springframework.beans.factory.support.DefaultSingletonBeanRegistry main (DefaultSingletonBeanRegistry.java:581) - 销毁 bean 上的方法 有名字 'org.springframework.context.annotation.internalScheduledAnnotationProcessor' 抛出异常 java.lang.IllegalStateException: ApplicationEventMulticaster 未初始化 - 之前调用“刷新” 通过上下文多播事件: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3a45c42a: 启动日期 [Mon Jun 19 22:41:11 CST 2017];上下文层次的根 在 org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 bing.AuthorityApplication.main(AuthorityApplication.java:40) [类/:?] 2017-06-19 22:41:13,407:ERROR org.springframework.beans.factory.support.DefaultSingletonBeanRegistry main (DefaultSingletonBeanRegistry.java:581) - 销毁 bean 上的方法 有名字 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor' 抛出异常 java.lang.IllegalStateException: ApplicationEventMulticaster 未初始化 - 之前调用“刷新” 通过上下文多播事件: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3a45c42a: 启动日期 [Mon Jun 19 22:41:11 CST 2017];上下文层次的根 在 org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 bing.AuthorityApplication.main(AuthorityApplication.java:40) [classes/:?] 2017-06-19 22:41:13,409:ERROR org.springframework.beans.factory.support.DefaultSingletonBeanRegistry main (DefaultSingletonBeanRegistry.java:581) - 销毁 bean 上的方法 有名字 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' 抛出异常 java.lang.IllegalStateException: ApplicationEventMulticaster 未初始化 - 之前调用“刷新” 通过上下文多播事件: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3a45c42a: 启动日期 [Mon Jun 19 22:41:11 CST 2017];上下文层次的根 在 org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556) [spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在 bing.AuthorityApplication.main(AuthorityApplication.java:40) [课程/:?] 2017-06-19 22:41:13,422:INFO org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer 主要(AutoConfigurationReportLoggingInitializer.java:101) -

启动 ApplicationContext 时出错。显示自动配置 报告启用“调试”后重新运行您的应用程序。 2017-06-19 22:41:13,663:错误 org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter 主要(LoggingFailureAnalysisReporter.java:42) -

***************************应用程序启动失败


说明:

方法methodValidationPostProcessor的参数1在 org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration 需要一个“javax.validation.Validator”类型的 bean,它不能 成立。 - 因为@ConditionalOnMissingBean(类型: javax.validation.Validator; SearchStrategy: all) 找到的bean 'mvcValidator'

行动:

考虑重新审视上述条件或定义一个 bean 类型 'javax.validation.Validator' 在你的配置中。

这是我的自定义验证配置,将消息源与 jsr303 集成

@Configuration
public class ValidationMessageConfig 

    @Bean
    public LocalValidatorFactoryBean mvcValidator(MessageSource messageSource) 
        LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
        factory.setValidationMessageSource(messageSource);
        return factory;
    

【问题讨论】:

请提供更多详细信息,并使用代码格式化按钮格式化您的输出。但是,就您当前的问题而言,您无法格式化输出,因为代码多于您所遇到问题的实际描述。我尝试自己格式化,但无法提交建议的编辑。 【参考方案1】:

不确定它如何/为什么与 1.5.3 一起使用,但您应该通过从 @Bean 注释方法返回一个来配置默认的 Validator。改写如下:

@Bean
public Validator mvcValidator(MessageSource messageSource) 
    LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
    factory.setValidationMessageSource(messageSource);
    return factory;

【讨论】:

感谢您的友好回复,但它不起作用。同样的错误。【参考方案2】:

我遇到了同样的问题并解决了这个问题。像这样重写你的配置。

@Configuration
public class ValidationMessageConfig extends WebMvcConfigurerAdapter 

    @Autowired
    private MessageSource messageSource;

    @Override
    public Validator getValidator() 
        LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
        factory.setValidationMessageSource(messageSource);
        return factory;
    

【讨论】:

以上是关于Spring Boot 从 1.5.3 升级到 1.5.4,自定义验证配置启动失败的主要内容,如果未能解决你的问题,请参考以下文章

从Spring Boot 1.5升级到2.0

Spring Boot 版本从 2.1.6 升级到 2.2.1 和 spring-cloud 问题

SpringBoot项目版本升级:从1.5.3升级到2.1.8版本

独立资源服务器(Spring Boot 2 + OAuth + JWT)在 Spring-boot 从 1.2.x 升级到 2.x 后给出 UsernameNotFoundException

将 Spring Boot 升级到 2.4.1

Spring Boot版本升级——mysql报错