创建名称为 'spring.datasource.CONFIGURATION_PROPERTIES' 的 bean 时出错

Error creating bean with name 'spring.datasource.CONFIGURATION_PROPERTIES'

我在启动我的 Spring 启动项目时遇到错误,异常片段如下

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is javax.validation.Validation
Exception: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)

尝试 @Autowired java.sql.Datasource bean 时发生错误。

但是,通过包含 Hibernate Validator 依赖关系解决了这个问题

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>

pom.xml 片段

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.3.3.RELEASE</version>
   <relativePath/> 
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
    <querydsl.version>4.0.1</querydsl.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-sql</artifactId>
        <version>${querydsl.version}</version>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-sql-codegen</artifactId>
        <version>${querydsl.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

我担心的是为什么有必要包含 hibernate-validator,如果有必要,为什么不将其作为 spring-boot-starter-jdbc 的传递依赖项包含在内?

编辑:按要求添加完整的堆栈跟踪

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoApplication': Injection of autowired dependencies failed; nested exception is o
rg.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.sql.DataSource com.example.DemoApplication.dataSource; nested exception is org.springframework
.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration': Injection of a
utowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.autoconfigure.jd
bc.DataSourceProperties org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.properties; nested exception is org.springframework.beans.fac
tory.BeanCreationException: Error creating bean with name 'spring.datasource.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is javax.validation.Validation
Exception: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
        at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
        at com.example.DemoApplication.main(DemoApplication.java:35)
        ... 6 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.sql.DataSource com.example.DemoApplication.dataSource; nested exception is org.spr
ingframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration': In
jection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.auto
configure.jdbc.DataSourceProperties org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.properties; nested exception is org.springframewo
rk.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is javax.validatio
n.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbedded
Configuration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springf
ramework.boot.autoconfigure.jdbc.DataSourceProperties org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.properties; nested exception is
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception
is javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your cl
asspath.
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
        ... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.autoconfigure.jdbc.DataSourceProperties org.springframe
work.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.properties; nested exception is org.springframework.beans.factory.BeanCreationException: Error creat
ing bean with name 'spring.datasource.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is javax.validation.ValidationException: Unable to create a Configura
tion, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 44 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested
exception is javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI)
to your classpath.
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
        ... 46 more
Caused by: javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to
 your classpath.
        at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:271)
        at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:223)
        at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor$LocalValidatorFactory.run(ConfigurationPropertiesBindingPostProcessor.java:441)
        at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.getValidator(ConfigurationPropertiesBindingPostProcessor.java:375)
        at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.determineValidator(ConfigurationPropertiesBindingPostProcessor.java:358)
        at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:31
7)
        at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:28
9)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
        ... 55 more

EDIT2:在 hibernate-validator

之前添加 mvn dependency:tree
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.3.3.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.2.5.RELEASE:compile
[INFO] |  |     +- org.springframework:spring-aop:jar:4.2.5.RELEASE:compile
[INFO] |  |     |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |     \- org.springframework:spring-expression:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- ch.qos.logback:logback-classic:jar:1.1.5:compile
[INFO] |  |  |  \- ch.qos.logback:logback-core:jar:1.1.5:compile
[INFO] |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.16:compile
[INFO] |  +- org.springframework:spring-core:jar:4.2.5.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.apache.tomcat:tomcat-jdbc:jar:8.0.32:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-juli:jar:8.0.32:compile
[INFO] |  \- org.springframework:spring-jdbc:jar:4.2.5.RELEASE:compile
[INFO] |     +- org.springframework:spring-beans:jar:4.2.5.RELEASE:compile
[INFO] |     \- org.springframework:spring-tx:jar:4.2.5.RELEASE:compile
[INFO] +- org.hsqldb:hsqldb:jar:2.3.3:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.3.3.RELEASE:test
[INFO] |  +- junit:junit:jar:4.12:test
[INFO] |  +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] |  |  \- org.objenesis:objenesis:jar:2.1:test
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  \- org.springframework:spring-test:jar:4.2.5.RELEASE:test
[INFO] +- com.querydsl:querydsl-sql:jar:4.0.1:compile
[INFO] |  +- com.querydsl:querydsl-core:jar:4.0.1:compile
[INFO] |  |  +- com.google.guava:guava:jar:18.0:compile
[INFO] |  |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  |  \- com.mysema.commons:mysema-commons-lang:jar:0.2.4:compile
[INFO] |  +- joda-time:joda-time:jar:2.8.2:compile
[INFO] |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  +- org.apache.servicemix.bundles:org.apache.servicemix.bundles.javax-inject:jar:1_2:compile
[INFO] |  \- com.infradna.tool:bridge-method-annotation:jar:1.13:compile
[INFO] \- com.querydsl:querydsl-sql-codegen:jar:4.0.1:provided
[INFO]    +- com.querydsl:querydsl-codegen:jar:4.0.1:provided
[INFO]    |  +- com.mysema.codegen:codegen:jar:0.6.6:provided
[INFO]    |  |  \- org.eclipse.jdt.core.compiler:ecj:jar:4.3.1:provided
[INFO]    |  +- javax.inject:javax.inject:jar:1:provided
[INFO]    |  \- org.reflections:reflections:jar:0.9.9:provided
[INFO]    |     +- org.javassist:javassist:jar:3.18.1-GA:provided
[INFO]    |     \- com.google.code.findbugs:annotations:jar:2.0.1:provided
[INFO]    \- com.querydsl:querydsl-sql-spatial:jar:4.0.1:provided
[INFO]       +- com.querydsl:querydsl-spatial:jar:4.0.1:provided
[INFO]       |  \- org.geolatte:geolatte-geom:jar:0.13:provided
[INFO]       \- com.vividsolutions:jts:jar:1.10:provided

您可以分别使用 HibernateSpring 的验证器。 如果你在你的代码中实现 @Valid 注释,你告诉 VM 验证将首先由 Hibernate 然后 Spring.

您可以通过取消实现 @Valid 注释来阻止 Hibernate 验证。

[INFO] +- com.querydsl:querydsl-sql:jar:4.0.1:compile
[INFO] |  +- com.querydsl:querydsl-core:jar:4.0.1:compile
[INFO] |  |  +- com.google.guava:guava:jar:18.0:compile
[INFO] |  |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  |  \- com.mysema.commons:mysema-commons-lang:jar:0.2.4:compile
[INFO] |  +- joda-time:joda-time:jar:2.8.2:compile
[INFO] |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  +- org.apache.servicemix.bundles:org.apache.servicemix.bundles.javax-inject:jar:1_2:compile
[INFO] |  \- com.infradna.tool:bridge-method-annotation:jar:1.13:compile

真正的罪魁祸首是 querydsl-sql,因为它正在拉入 validation-apivalidation-api 的存在反过来会打开属性验证。

但是,顾名思义,validation-api 只是 API,您仍然需要一个实现来启用验证。那不可用,因此您会收到错误消息。要修复您有 2 个选项。

  1. 包括 hibernate-validator 作为依赖项(如您所见有效)
  2. querydsl-sql 依赖项中排除 validation-api

这两种解决方案都行得通。第一个的好处是 Spring Boot 现在可以检测您的配置中的错误,而没有它则不能。缺点是你又有了另一个依赖。