简体   繁体   English

无法读取application.yml文件

[英]application.yml file cannot be read

Env: Eclipse + sts 环境:Eclipse + sts

I new a Spring Starter Project, I choose JDBC API, MS SQL Server Driver, Spring Web Starter. 我新建了一个Spring Starter项目,选择JDBC API,MS SQL Server驱动程序,Spring Web Starter。

The first time it creates, errors like this: 第一次创建时,会出现以下错误:

Multiple annotations found at this line: - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-resources-plugin:3.1.0:testResources (execution: default-testResources, phase: process-test-resources) - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-resources-plugin:3.1.0:resources (execution: default-resources, phase: process-resources)

I ignore it. 我不理。 I add resources in the pom file, 我在pom文件中添加资源,

    <resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

Then I add SQL Server thing in src/main/resources/application.yml spring: datasource: driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver password: sly url: jdbc:sqlserver://localhost:1433;DatabaseName=msdb username: sa 然后我在src / main / resources / application.yml spring: datasource: driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver password: sly url: jdbc:sqlserver://localhost:1433;DatabaseName=msdb username: sa添加SQL Server内容spring: datasource: driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver password: sly url: jdbc:sqlserver://localhost:1433;DatabaseName=msdb username: sa

but I run @SpringBootApplication, it give infos 但是我运行@SpringBootApplication,它提供信息

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 have tried application.properties, it doesnot work either. 我试过了application.properties,它也不起作用。

spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=msdb
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=sa
spring.datasource.password=sly

<include>**/*.properties</include>

Then I try another way, I create file DatasourceConfig.java, and code it 然后尝试另一种方法,创建文件DatasourceConfig.java,并对其进行编码

package com.ycr;

import javax.sql.DataSource;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DatasourceConfig {
    @Bean
    public DataSource datasource() {
        return DataSourceBuilder.create().driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                .url("jdbc:sqlserver://localhost:1433;DatabaseName=msdb").username("sa").password("sly").build();
    }
}

and the it works. 和它的作品。 I wonder why it can not read my yml file?(change yml to properties doesnot work.) 我不知道为什么它无法读取我的yml文件?(将yml更改为属性不起作用。)

I wonder why it can not read my yml file?(change yml to properties doesnot work.) 我不知道为什么它无法读取我的yml文件?(将yml更改为属性不起作用。)

I wonder why it can not read my yml file?(change yml to properties doesnot work.) 我不知道为什么它无法读取我的yml文件?(将yml更改为属性不起作用。)

请在您的yml文件中将driverClassName更改为driver-class-name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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