Java 数据源的 Spring Boot 自动配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41741135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:38:21  来源:igfitidea点击:

Spring Boot auto configuration for datasource

javahibernatemavenspring-boot

提问by Fergus MacDubh

I try to create Spring Boot app with Hibernate 5 and Postgres 9. Now I have next error:

我尝试使用 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

Though, I have added spring.datasource.* properties:

不过,我添加了 spring.datasource.* 属性:

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=root

My pom.xml:

我的 pom.xml:

<properties>
    <hibernate.version>5.2.6.Final</hibernate.version>
    <spring.data.version>1.10.6.RELEASE</spring.data.version>
</properties>

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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring.data.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.12</version>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1212</version>
    </dependency>
</dependencies>

Report:

报告:

=========================
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)

Any ideas? In most tutorials it all is very standard and very simple, it seems I miss some little part..

有任何想法吗?在大多数教程中,这一切都非常标准且非常简单,似乎我错过了一些小部分..

采纳答案by g00glen00b

You're missing several classes (mostly pool related) on your classpath. The easiest solution is to use the Spring boot starter for JPA, which is:

您的类路径中缺少几个类(主要与池相关)。最简单的解决方案是使用 Spring boot starter for JPA,即:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

If you do this, you can remove the following dependencies since they're all part of the starter:

如果这样做,您可以删除以下依赖项,因为它们都是 starter 的一部分:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>${spring.data.version}</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate.version}</version>
</dependency>

The alternative solution is to manually add a pool provider to your classpath, the default of spring-boot-starter-data-jpais tomcat-jdbc(Hikari for Spring boot 2.x) but you can use any connection pool provider you want that is listed in the documentation.

另一种解决方案是手动将池提供程序添加到您的类路径中,spring-boot-starter-data-jpa的默认值是tomcat-jdbc(用于 Spring boot 2.x 的 Hikari)但您可以使用任何您想要的连接池提供程序在文档中列出。

回答by praveen kumar

well the above solution might worked but in my case with weblogic 11g above solution could not help me. so attaching this solution so anyone elsestruggling with this problem find it useful.

好吧,上述解决方案可能有效,但就我而言,上面的 weblogic 11g 解决方案无法帮助我。所以附上这个解决方案,以便其他任何正在解决这个问题的人都觉得它很有用。

solution:

解决方案:

Adding DataSource bean configuration in component scan classpath will fix this for servlets 2.5 like weblogic 10.3.6(11g) like below

在组件扫描类路径中添加 DataSource bean 配置将为 servlets 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;
       }

}