2

我已经将其创建application-local.jsonsrc/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).
4

1 回答 1

1

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

因此,在您的情况下,Spring 将寻找应用程序本地。yml应用程序本地。属性以读取配置文件特定的属性。

但是在这里,您已将属性文件定义为应用程序本地。json这就是为什么 spring 不读取值并且您遇到异常的原因。

解决方案 创建应用程序本地。yml应用程序本地。属性并粘贴您的内容并尝试。它应该工作。

这是示例数据库配置。

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
于 2018-06-18T16:40:08.337 回答