【问题标题】:Failed to configure a DataSource: 'url'无法配置数据源:'url'
【发布时间】:2020-10-14 07:17:10
【问题描述】:

我启动了新的 Spring Boot 应用程序,但在尝试运行它时遇到了以下问题:

localhost 8080 已在使用中

在此之前,我有一个旧的 eeproject,我决定删除它并尝试删除那里的 tomcat 属性。在我尝试重新运行我的 Spring 项目后,当我开始运行它时:

未能配置数据源:未指定“url”属性并且无法配置嵌入式数据源。 原因:无法确定合适的驱动程序类。

我该怎么办?

我的主要课程:

package com.example.sweater;

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

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
<?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 http://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.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->

</parent>
<groupId>com.example</groupId>
<artifactId>serving-web-content</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>serving-web-content</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-mustache</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</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>
</dependencies>

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

</project>

【问题讨论】:

  • 请用 application.properties 和主类更新问题
  • 抱歉,applcation.properties 是什么意思?这是一个非常新鲜的项目,我只添加了 maven 依赖项
  • 请用你的 pom.xml 更新问题
  • 我按照你的要求更新它,我希望这将有助于找出问题所在

标签: java spring maven intellij-idea localhost


【解决方案1】:

从 pom 文件中删除以下依赖项。

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

如果 Spring-Boot 在 classpath 上找到 spring-boot-starter-data-jpa 依赖项,它会自动尝试查找数据源。

【讨论】:

  • 非常感谢,我没有注意到,我忘记添加 jpa 依赖项,也忘记添加我的 postgresql 依赖项,祝你有美好的一天!
【解决方案2】:
  1. 打开您的终端并运行sudo lsof -i tcp:8080 以获取PID,然后使用kill -9 xxxxx 终止进程,其中“xxxxx”是您的PID,现在您可以在端口8080 重新启动您的应用程序。 对于 Windows,输入 netstat -aon | find "8080" 并使用 taskkill /F /PID xxxxx 终止进程,其中“xxxxx”是您的 PID。
  2. 如果您尚未添加 SQL 依赖项,请先添加 SQL 依赖项(例如 MySQL 或 PostgreSQL),然后在您的 resources->application.properties 添加您的 SQL 属性。 如果您不打算使用数据库,请删除 Spring JPA 依赖项。

【讨论】:

  • 我做到了,谢谢,但之后我遇到了一个新问题> 无法配置数据源:未指定“url”属性,并且无法配置嵌入式数据源。原因:无法确定合适的驱动程序类
  • 如果你还没有添加 SQL 依赖,先添加一个 SQL 依赖(比如 MySQL 或 PostgreSQL),然后在你的 resources->application.properties 添加你的 SQL 属性
  • 如果您不打算使用数据库,请删除 Spring JPA 依赖项。
猜你喜欢
  • 1970-01-01
  • 2023-02-21
  • 2022-07-21
  • 2021-05-14
  • 2019-03-25
  • 2019-08-24
  • 2021-03-18
  • 2022-12-25
  • 2018-12-15
相关资源
最近更新 更多