我有一个使用Spring数据JPA的以下Spring启动项目。我的休息控制器注释了以下注释 - :
@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>
答案 0 :(得分:0)
只有在类路径中有相应的启动器依赖项时,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>
取决于您使用的数据库。
答案 1 :(得分:0)
根据你的pom.xml,我假设你使用的是Postgres。确保在application.properties文件中配置了数据源。例外可能表明它配置错误。
spring.datasource.url=jdbc:postgresql://localhost/testdb
spring.datasource.username=postgres
spring.datasource.password=123