指尖世界

分享的乐趣

Spring-boot启动报 Cannot bind to SpringApplication错误

类别: 站内精品文章 更新时间: 2017-10-07

今天晚上在把原来一些工程从eclipse迁移到intellij IDEA,结果,启动项目的时候,发现启动报错,错误信息如下:

2017-10-06 22:24:42.998  INFO 248 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5f058f00: startup date [Fri Oct 06 22:24:42 EAT 2017]; root of context hierarchy
2017-10-06 22:24:44.048  INFO 248 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-10-06 22:24:44.198  INFO 248 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a24549ed] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-10-06 22:24:48.318 ERROR 248 --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Cannot bind to SpringApplication
	at org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:233) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:186) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:171) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:157) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:296) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at com.v8en.studio.registration.center.RegApplication.main(RegApplication.java:17) [classes/:na]
Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors
Field error in object 'spring.main' on field 'bannerMode': rejected value [false]; codes [typeMismatch.spring.main.bannerMode,typeMismatch.bannerMode,typeMismatch.org.springframework.boot.Banner$Mode,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.main.bannerMode,bannerMode]; arguments []; default message [bannerMode]]; default message [Failed to convert property value of type 'java.lang.Boolean' to required type 'org.springframework.boot.Banner$Mode' for property 'bannerMode'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Boolean' to required type 'org.springframework.boot.Banner$Mode' for property 'bannerMode': no matching editors or conversion strategy found]
	at org.springframework.boot.bind.PropertiesConfigurationFactory.checkForBindingErrors(PropertiesConfigurationFactory.java:359) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:276) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	at org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:230) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	... 13 common frames omitted


经过在网上的搜寻,终于找到了原因竟然是存在Bug,相关地址如下:

http://www.logicbig.com/examples/spring-boot/yaml-banner-off-exception/


至于官网我并没有去做查证,但是修改后确实就没有问题了。介于国内某些网上无法访问的原因,这里站长大概描述下问题并给出解决方案:

首先看下我们的配置:

security:
  basic:
    enabled: true
  user:
    name: simon
    password: 1234
server:
  port: 8761
eureka:
  server:
    enable-self-preservation: false
  client:
    healthcheck:
      enabled: true
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://user:password@localhost:8761/eureka/
spring:
  main:
    banner-mode: off


问题处在banner-mode这个配置项,YAML将这个配置项作为boolean类型,但是,Spring却试图将这个boolean值转成枚举类型Banner.Mode


解决办法也很简单,把这个值设置成非boolean类型,如:

spring:
  main:
    banner-mode: console