找不到Spring Config配置文件

时间:2018-06-18 12:18:43

标签: spring-boot spring-config

我已将此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).

1 个答案:

答案 0 :(得分:1)

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

因此,在您的情况下,Spring会查找 application-local。 yml application-local。 properties 阅读个人资料特定属性。

但是在这里,你已经将属性文件定义为应用程序本地。 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