Spring 找不到配置文件

Spring Config profile file is not found

我已将此 application-local.json 创建为 src/main/resources:

{
  "spring": {
    "datasource": {
      "url": "jdbc:postgresql://xxx:yyy/db",
      "username": "xxx",
      "password": "xxx",
      "driverClassName": "org.postgresql.Driver"
    },
    "profiles": {
      "active": "local"
    }
  }
}

另一方面,apliication.yml:

spring:
  jpa:
    generate-ddl: false
    show-sql: true
    properties:
      hibernate:
        format_sql: true
        jdbc:
          lob:
            non_contextual_creation: true

  profiles:
    active: local

management:
  security:
    enabled: false
  endpoints:
    web:
      exposure:
        include: '*'

---

spring:
  profiles: local

server:
  port: 8092

目前,我收到这条消息:

***************************
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).

当 Spring 应用程序运行时,它从 application.yml->spring->profiles->active 的值加载属性。 Spring 仅支持 yml.properties 文件作为源。

因此,在您的情况下 Spring 将查找 application-local.ymlapplication -local.properties 读取配置文件特定 属性。

但是在这里,您已将 属性 文件定义为 application-local.json 并且spring 没有读取值并且您遇到异常的原因。

解决方法 创建 application-local.ymlapplication-local.properties 并粘贴您的内容并尝试。应该可以。

这是示例数据库配置。

spring.datasource.url=jdbc:mysql://localhost:3306/_schema name_
spring.datasource.username=_username_
spring.datasource.password=_password_
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql = true
logging.level.org.hibernate.SQL=debug