spring boot 配置分离

本文介绍了Spring Boot如何实现配置分离,通过设置环境变量可以改变默认的配置文件名,或者指定配置文件的位置。当目录包含子目录时,需要以`application`结尾,系统会自动加载包括特定环境的配置文件。同时,`spring.config.additional-location`指定的文件路径会覆盖默认值,允许设定默认配置并按需运行时覆盖。
Spring Boot默认为我们提供了一个配置之地!只要按照指定的名称创建属性文件,并放置在指定位置,Spring Boot会自动解析其内容。

关于Spring Boot的常用的配置分离主要有以下几种方式:
1、命令行参数
2、通过jar外部的application-{profile}.properties激活对应系统环境的配置生效
3、通过spring.config.location参数指定配置文件所在位置
4、 默认情况下Spring Boot会从以下位置寻找application.properties文件,并把属性加入到Spring Environment中。
1)当前目录的一个config/子目录
2) 当前目录
3) 一个classpath下的config/包
4) classpath根路径(root)
上述列表中的位置优先级自上而下递减,同样的属性配置,高位置的会覆盖低位置的.

引用官方的文档说明:

If you don’t like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).

$ java -jar myproject.jar --spring.config.name=myproject

or

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
[Warning]

spring.config.name and spring.config.location are used very early to determine which files have to be loaded so they have to be defined as an environment property (typically OS env, system property or command line argument).

If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and will be overridden by any profile-specific properties.

The default search path classpath:,classpath:/config,file:,file:config/ is always used, irrespective of the value of spring.config.location. This search path is ordered from lowest to highest precedence (file:config/ wins). If you do specify your own locations, they take precedence over all of the default locations and use the same lowest to highest precedence ordering. In that way you can set up default values for your application in application.properties (or whatever other basename you choose with spring.config.name) and override it at runtime with a different file, keeping the defaults.


默认将从以下位置加载配置文件spring.config.location=classpath:application.properties,classpath:config/application.properties,多个文件路径以逗号间隔。如果路径以最后为目录,必须以"/"结尾。spring.config.location=classpath:config/,那么spring.config.name会自动追加到后面!一旦设定了spring.config.location属性,上述的默认位置将不起作用!因为你要修改的是application.properties的名称,所以上面的两个属性建议通过命令行的方式配置,如:
java -jar trade-web-0.0.1-SNAPSHOT-f1f86f1.jar --spring.config.location=/var/www/config/ --spring.config.name=application.properties
java -jar trade-web-0.0.1-SNAPSHOT-f1f86f1.jar --spring.config.location=/var/www/config/application.properties
项目目录结构如图所示:

附上启动脚本trade.sh,可以根据需要修改黄色部分文字:
#!/bin/sh
#kill java pid
pidlist=`ps -ef|grep java|grep "trade"|awk '{print $2}'`
if [ "$pidlist" = "" ]
   then
       echo "no java application pid alive!"
else
  echo "java application pid list :$pidlist"
  kill -9 $pidlist
  echo "kill java $pidlist:"
  echo "service stop success"
fi
echo "service start ..."
#find ./ -name "*.jar" | xargs sh trade.sh
jarpackage=`find ./ -name "*.jar"`
echo " jar package name: $jarpackage"
nohup java -jar $jarpackage --spring.config.location=/var/www/config/application.properties > /data/logs/tomcat/trade-web.log 2>&1 & tail -f /data/logs/tomcat/trade-web.log





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值