我尝试使用Hibernate 5和Postgres 9创建Spring Boot应用程序.现在我有下一个错误:
Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found. - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name' - Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans
虽然,我添加了spring.datasource.*属性:
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres spring.datasource.username=postgres spring.datasource.password=root
我的pom.xml:
5.2.6.Final 1.10.6.RELEASE org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE org.springframework.boot spring-boot-autoconfigure org.springframework.boot spring-boot-starter-web org.springframework spring-context ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-orm ${spring.version} org.springframework.data spring-data-jpa ${spring.data.version} org.hibernate hibernate-core ${hibernate.version} org.projectlombok lombok 1.16.12 org.postgresql postgresql 9.4.1212
报告:
========================= AUTO-CONFIGURATION REPORT ========================= Positive matches: ----------------- AopAutoConfiguration matched: - @ConditionalOnClass found required classes 'org.springframework.context.annotation.EnableAspectJAutoProxy', 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition) - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition) AopAutoConfiguration.JdkDynamicAutoProxyConfiguration matched: - @ConditionalOnProperty (spring.aop.proxy-target-class=false) matched (OnPropertyCondition) DataSourceAutoConfiguration matched: - @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (OnClassCondition) DataSourceAutoConfiguration#dataSourceInitializer matched: - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer; SearchStrategy: all) did not find any beans (OnBeanCondition) DataSourceTransactionManagerAutoConfiguration matched: - @ConditionalOnClass found required classes 'org.springframework.jdbc.core.JdbcTemplate', 'org.springframework.transaction.PlatformTransactionManager' (OnClassCondition) DataSourceTransactionManagerAutoConfiguration.TransactionManagementConfiguration matched: - @ConditionalOnMissingBean (types: org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; SearchStrategy: all) did not find any beans (OnBeanCondition) ... DataSourceAutoConfiguration.EmbeddedDatabaseConfiguration: Did not match: - EmbeddedDataSource did not find embedded database (DataSourceAutoConfiguration.EmbeddedDatabaseCondition) DataSourceAutoConfiguration.PooledDataSourceConfiguration: Did not match: - AnyNestedCondition 0 matched 2 did not; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.PooledDataSourceAvailable PooledDataSource did not find supported DataSource; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.ExplicitType @ConditionalOnProperty (spring.datasource.type) did not find property 'type' (DataSourceAutoConfiguration.PooledDataSourceCondition) DataSourceAutoConfiguration.TomcatDataSourceJmxConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.apache.tomcat.jdbc.pool.DataSourceProxy' (OnClassCondition) DataSourcePoolMetadataProvidersConfiguration.CommonsDbcp2PoolDataSourceMetadataProviderConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.apache.commons.dbcp2.BasicDataSource' (OnClassCondition) DataSourcePoolMetadataProvidersConfiguration.CommonsDbcpPoolDataSourceMetadataProviderConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.apache.commons.dbcp.BasicDataSource' (OnClassCondition) DataSourcePoolMetadataProvidersConfiguration.HikariPoolDataSourceMetadataProviderConfiguration: Did not match: - @ConditionalOnClass did not find required class 'com.zaxxer.hikari.HikariDataSource' (OnClassCondition) DataSourcePoolMetadataProvidersConfiguration.TomcatDataSourcePoolMetadataProviderConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.apache.tomcat.jdbc.pool.DataSource' (OnClassCondition) DataSourceTransactionManagerAutoConfiguration.DataSourceTransactionManagerConfiguration: Did not match: - @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)
有任何想法吗?在大多数教程中,它都非常标准且非常简单,似乎我错过了一些小部分..
您在类路径上缺少几个类(主要与池相关).最简单的解决方案是为JPA使用Spring启动启动器,即:
org.springframework.boot
spring-boot-starter-data-jpa
如果这样做,您可以删除以下依赖项,因为它们都是启动器的一部分:
org.springframework
spring-context
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework.data
spring-data-jpa
${spring.data.version}
org.hibernate
hibernate-core
${hibernate.version}
另一种解决方案是手动将池提供程序添加到类路径中,spring-boot-starter-data-jpa的缺省值是tomcat-jdbc(Hikari for Spring boot 2.x)但是你可以使用你想要的任何连接池提供程序列在文档中.
以上解决方案可能会起作用,但在我的情况下使用weblogic 11g上面的解决方案无法帮助我.因此,附加此解决方案,以便任何人解决这个问题,发现它很有用.
解:
在组件扫描类路径中添加DataSource bean配置将修复servlet 2.5,如weblogic 10.3.6(11g),如下所示
@Configuration public class DBConfig{ @Bean public DataSource dataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); dataSource.setUrl("your url"); dataSource.setUsername( "username" ); dataSource.setPassword( "password" ); return dataSource; } }