SpringBoot2无法自动配置DataSource:未指定“spring.datasource.url”,也无法自动配置嵌入式数据源

时间:2018-04-16 08:59:32

标签: java spring spring-boot

我的项目使用springBoot2,它不需要连接数据库,但是springboot自动配置数据源并在启动项目时抛出异常。 我添加了执行,但它不起作用

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class
})
@ImportResource("classpath:spring-config-platform.xml")
public class JunoIotPlatformBoot1Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(JunoIotPlatformBoot1Application.class);
        Map<String,Object> map = new HashMap<>();
        map.put("server.port",8091);
        app.setDefaultProperties(map);
        app.run(args);
    }

因为我将旧的spring项目(xml)用于springboot项目,所以ImportResource是必需的。 日志是:

    2018-04-16 16:19:01,141][restartedMain][WARN][org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:557)] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

.....

    [2018-04-16 16:19:01,221][restartedMain][INFO][org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:180)] Stopping service [Tomcat]
    [2018-04-16 16:19:01,259][restartedMain][ERROR][org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter.report(LoggingFailureAnalysisReporter.java:42)] 

    ***************************
    APPLICATION FAILED TO START
    ***************************

    Description:

    Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.

    Reason: Failed to determine a suitable driver class


    Action:

    Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

2 个答案:

答案 0 :(得分:1)

你应该检查你的构建脚本,看起来你有一些不需要的启动器(某些东西以spring-boot-starter-data-...开头)

答案 1 :(得分:0)

在pom.xml中设置了JPA依赖项而没有数据库依赖项时,也会发生这种情况。

对我来说,用JPA初始化项目还不够-我需要添加相关的数据库,例如 H2

要解决此问题,请将您的数据库依赖项添加到pom中,例如对于 H2

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>