java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot外部配置文件:spring.config.location

Spring Boot外部配置文件:spring.config.location使用技巧

作者:未发哦京东发

还在为Spring Boot配置文件管理头疼?本文详解spring.config.location的两种用法:配置文件中引用和命令行指定,包括优先级陷阱与实战经验,帮你轻松加载任意位置的外部配置文件,告别配置混乱

一般使用spring.config.location引用为任意位置配置文件 

1.我的项目目录结构

2.第一种在配置文件中spring.config.location引用了另一个配置文件

注:

因为spring.config.location优先级低,执行application.yml时此属性已经有默认值,所以放到内部不会生效 

3.第二种命令行启动时指定spring.config.location引用的配置文件

注:

这种方式不会引用jar包内的配置文件,只会引用 --spring.config.location指定的配置文件

java -jar zzxt-gather.jar --spring.config.location=D:\aaa\zzxt-gather\application.yml,D:\aaa\application.yml
java -jar zzxt-gather.jar --spring.config.location=.\application.yml,..\application.yml

4.两个配置文件 

‪D:\aaa\zzxt-gather\application.yml

server:
    address:
    port: 9010
    servlet:
        context-path: /zzxt-gather
        session:
          timeout: 1800
#aa: aa
#==== 项目自用配置
service.url: http://localhost:9020/zzxt-service
#voc文件路径
vocs.dirPath: D:\\test\\
#voc文件另存路径
vocs.cpDirPath: D:\\test2\\
   #port:VOC Server监听的端口
vocs.server.port:      9021
   #charset:生产环境应为“UTF-8”
vocs.server.charset:   GBK          
aa: aa
spring.config.location: D:\aaa\application.yml

‪D:\aaa\application.yml

spring:
  #MySQL数据库
  datasource:
    main:
      jdbc-url: jdbc:mysql://192.168.2.101:3306/zzxt_main?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8
      username: gmp
      password: bw.2018

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文