【问题标题】:Spring boot - Why is the exclude required when manually configuring Hibernate?Spring boot - 为什么手动配置 Hibernate 时需要排除?
【发布时间】:2017-10-10 04:07:58
【问题描述】:

我有一个使用 Spring 数据 JPA 的以下 Spring 引导项目。我的休息控制器使用以下注释进行注释:

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@ComponentScan({ "com.foo.bar"})
public class RESTService {

我的问题是为什么 @EnableAutoConfiguration 中需要排除参数?如果我在没有排除项的情况下启动应用程序,则会出现以下异常-:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

现在我在我的项目中手动配置 Hibernate。

我的理由是,由于 Spring Boot 在类路径上看到 spring 数据,它会尝试自动配置 JDBC 和 Hibernate JPA。但是为什么不尝试自动配置 Mongo 或任何其他数据库解决方案呢?

有人可以帮我理解这里发生了什么吗?

我的 POM 文件是-:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.foo.bar</groupId>
  <artifactId>Project</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>REST Service</name>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.javacsv</groupId>
            <artifactId>javacsv</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.1.0</version>
        </dependency>
        <!-- 
        <dependency>
                <groupId>org.fosstrak.epcis</groupId>
                <artifactId>epcis-repository</artifactId>
                <version>0.5.0</version>
            </dependency>
             -->
             <dependency>
                <groupId>org.fosstrak.epcis</groupId>
                <artifactId>epcis-repository</artifactId>
                <version>0.5.0</version>
                <scope>system</scope>
                <systemPath>${project.basedir}/lib/epcis-commons-0.5.0.jar</systemPath>
            </dependency>
    </dependencies>
    <properties>
        <java.version>1.7</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
                    <mainClass>com.foo.bar.RESTService</mainClass>
                    <addResources>true</addResources>
                    <layout>JAR</layout>
                  </configuration>
                <executions>
                    <execution>
                      <goals>
                        <goal>repackage</goal>
                      </goals>
                    </execution>
              </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 能否请您发布您的 pom 文件? @SpringBootApplication 应该放在主应用程序类上。 RESTService 类是你的主类吗?
  • @MrKiller21 添加了我的 POM 文件
  • Spring Boot 只会配置它在类路径中找到的内容。如果你没有 mongo 为什么需要配置 mongo。

标签: java hibernate spring-boot


【解决方案1】:

如果你的类路径中有相应的启动器依赖项,Spring 只会自动配置 Mongo。

例如:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>1.3.2.RELEASE</version>
        </dependency>

关于您发布的异常,它表示您尚未配置数据库驱动程序。你需要在你的属性中进行一些类似的操作:

spring.datasource.driver-class-name: oracle.jdbc.pool.OracleDataSource
spring.datasource.url: jdbc:oracle:thin:@<host>:1521:<schema>
spring.datasource.username: <user>
spring.datasource.password: <password>

当然取决于您使用的数据库。

【讨论】:

  • 你确定吗? JPA 的自动配置是什么?
  • 我稍微改变了解释。我希望现在更清楚了。
【解决方案2】:

根据您的 pom.xml,我假设您使用的是 Postgres。确保在 application.properties 文件中配置了数据源。异常可能表明它配置错误。

spring.datasource.url=jdbc:postgresql://localhost/testdb
spring.datasource.username=postgres
spring.datasource.password=123

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 2019-03-29
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多