繁体   English   中英

Spring Boot-为什么在手动配置Hibernate时需要排除?

[英]Spring boot - Why is the exclude required when manually configuring Hibernate?

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

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

我的问题是,为什么@EnableAutoConfiguration中需要exclude参数? 如果我在不排除的情况下启动应用程序,则会收到以下异常:

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>

如果您在类路径中具有相应的启动器依赖项,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>

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM