【问题标题】:SpringBoot Load profile properties from classpathSpring Boot 从类路径加载配置文件属性
【发布时间】:2020-06-05 01:21:12
【问题描述】:

我的问题:

是否可以在启动期间从类路径加载配置application-persist.yml

application-persist.yml 是我的外部库的一部分,包含 spring.datasource 配置

myexternallib.jar
- config
  - application-persist-dev.yml
    spring:
      datasource:
        jdbc-url: url-to-dev-database
        username: xxx
        password: xxx
        driver-class-name: org.postgresql.Driver
  - application-persist-prod.yml
    spring:
      datasource:
        jdbc-url: url-to-prod-database
        username: xxx
        password: xxx
        driver-class-name: org.postgresql.Driver

在我的 Spring Boot 项目中,myexternallib.jar 作为依赖项包含在内 我想从类路径加载配置:

我项目的application.yml

spring:
  profiles:
    include: persist
    active: dev, persist-dev

问题是:当我启动我的应用程序时,启动失败并出现异常:

Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.

因为在启动时没有正确读取application-persist-dev.yml。

我该如何解决?

【问题讨论】:

    标签: spring-boot configuration datasource


    【解决方案1】:

    Spring Boot's reference documentation此时已经很清楚了:

    java -jar myproject.jar --spring.config.location=classpath:/application-persist.yml
    

    【讨论】:

    • Spring 默认从类路径 /config 加载属性。因此无需配置特定位置。我的 yml 文件在类路径中,所以它应该加载。不是吗?
    猜你喜欢
    • 2019-03-04
    • 2018-12-19
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多