【问题标题】:spring boot failed to start the application, database url not specifiedspring boot 启动应用程序失败,未指定数据库url
【发布时间】:2020-04-13 23:18:28
【问题描述】:

我是spring的初学者,正在学习spring batch的基础知识。

根据初始培训,我可以使用基于 Map 的 JobRepository 启动应用程序,方法是使用以下注释:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

现在我正在尝试使用本地 postgresql(不使用 JPA)。

但它失败并出现以下错误:

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

Description:

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

Reason: Failed to determine a suitable driver class

配置文件如下:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.spring</groupId>
    <artifactId>hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-world</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5100/helloworld
spring.datasource.username=someuser
spring.datasource.password=somepasswd
spring.datasource.driver-class-name=org.postgresql.Driver
# Added below 2 based on some other stackoverflow searches
spring.datasource.platform=postgres
spring.batch.initialize-schema=always

请注意,我不想使用 JPA,因此没有在 pom.xml 中提供依赖项。

主类:

package io.spring.batch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }

}

我尝试编译、重新编译、使缓存无效并重新启动 (intellij),但似乎我的 application.properties 根本没有被读取。

本地 postgres 正在运行,能够通过客户端 (DBeaver/pgadmin) 和命令行连接它。

我们不能在不指定 JPA 依赖项的情况下运行它吗? 如果可以,上面有什么问题?

附上项目结构和错误的快照。

【问题讨论】:

  • 您是否在控制台中看到类似这样的日志:“已加载配置文件 'file:/somewhere in your local path/target/classes/application.properties' (classpath:/application.properties)” .至少您可以确保加载了正确的 applicaiton.properties...
  • 请在HelloWorldApplication 上使用@EnableBatchProcessing?另外请确保项目正在正确构建
  • Can't we run it without specifying JPA dependencies?:是的,不需要 jpa。 Failed to determine a suitable driver class: 你确定驱动 jar 在你的类路径中吗?

标签: java spring-boot spring-batch


【解决方案1】:

而不是

   spring.datasource.driver-class-name=org.postgresql.Driver

使用

   spring.datasource.platform=postgres

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 2017-05-12
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2021-06-06
    相关资源
    最近更新 更多