2016-04-29 42 views
1

我已經遇到錯誤豆,同時啓動下面錯誤創建名爲「spring.datasource.CONFIGURATION_PROPERTIES」

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) 

錯誤我的春節,引導項目,異常片段。

的問題卻被包括Hibernate驗證依賴

<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:treehibernate-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 
+0

你可以添加整個堆棧跟蹤而不是一個片段。我期望驗證器是'spring-boot-starter'的一部分(因爲配置類具有驗證應用程序或者它是可選的。我懷疑其中一個依賴關係拉入了'javax.validation',它會觸發驗證,這又需要一個實現。 –

+0

編輯並添加完整的堆棧跟蹤。 –

+0

在添加hibernate-validator依賴項之前,提供'mvn dependency:tree'的輸出也是有用的。 –

回答

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-api拉動。 validation-api的存在反過來開啓屬性的驗證。

但是,顧名思義,validation-api只是API,您仍然需要一個實現來啓用驗證。這是不可用的,所以你得到一個錯誤。要解決你有2個選項。

  1. 附上hibernate-validator作爲依賴(這工作,你所看到的)
  2. querydsl-sql依賴排除validation-api

兩種解決方案都可以使用。第一個好處是,Spring Boot現在可以檢測到配置中的錯誤,而沒有它不能。缺點是你有另一個依賴關係。

1

您可以同時使用休眠的和的驗證器。 如果您在您的代碼中實施了@Valid註釋,您告訴VM將首先通過休眠,然後彈簧進行驗證。

您可以通過執行@Valid註釋來防止休眠驗證。

相關問題