创建名为“spring.datasource.CONFIGURATION_PROPERTIES”的bean时出错

时间:2016-04-29 12:51:30

标签: spring maven spring-boot spring-jdbc

我在启动Spring Boot Project时遇到错误,

下面的Exception片段
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$1.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$1.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$1.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$1.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:在mvn dependency:tree之前添加hibernate-validator

[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

2 个答案:

答案 0 :(得分:2)

[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,您仍然需要一个实现来启用验证。这是不可用的,所以你得到一个错误。要解决此问题,您有两个选择。

  1. 包含hibernate-validator作为依赖项(这可以正常工作)
  2. validation-api依赖项中排除querydsl-sql
  3. 任何一种解决方案都可行。第一个好处是Spring Boot现在可以检测配置中的错误,而没有它不能。缺点是你有另一个依赖。

答案 1 :(得分:1)

您可以分别使用 Hibernate Spring 的验证器。 如果在代码中实现@Valid注释,则告诉VM首先通过 Hibernate 然后 Spring 进行验证。

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