简体   繁体   中英

Spring Boot project can't see data for database connection, written in application.yml

I'm writing a Java project in IntelliJ IDEA Community IDE. The technologies stack of this project:

  • Spring Boot;
  • Spring Data JPA;
  • MySQL.

I have the application.yml file in project to connect with database, but when I run my app, I get an error:

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

I'm at a loss why my project doesn't see my application.yml file, which locates in /src/main/resources/database path in project.

Any ideas?

application.yml:

spring:
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/map
    username: root
    password: abcd

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 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.1.8.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>example.com</groupId>
    <artifactId>geo-informer</artifactId>
    <version>1.0</version>
    <name>Geo Informer</name>
    <description>
        The Spring Boot app, which makes interaction with the OpenStreetmap Nominatim API
        to work with geographic data and store it to the MySQL database
    </description>

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

    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Data JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Spring Boot Maven plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This project is located here:

https://github.com/OlegSandro/geo-informer/tree/dev

Since the application.yml is not directly stored in src/main/resources it is not loaded.

In your EntryPoint.java file, you can add @PropertySource annotation and provide the path of your application.yml or you should store the file directly under the resources folder.

The application.yml or application.properties file should be in your classpath when the project is build. Spring boot has designed its structure such that when a maven build is executed, the property files from src/main/resources are placed into the classes directory with the other source files.

Therefore you need to directly place these files in src/main/resources

If you still wish to have the files elsewhere, you can point to the location with @PropertySource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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