默认使用application.properties的配置;
①properties文件的多环境支持(yml文件与此相同)
application.properties
server.port=8080 spring.profiles.active=pro //此处为pro时 打包后启动jar包 默认就是pro配置(除非指定环境启动 java -jar ***.jar --spring.profiles.test)
application-dev.properties
server.port=8081
application-test.properties
server.port=8082
application-forecast.properties
server.port=8083
application-pro.properties
server.port=8084
②yml单文件多文档块方式的多环境(我试了试不行,最好还是用properties多文件 和 yml多文件的形式)
server: port: 8080 spring: profiles: active: pro --- #开发环境 server: prot: 8081 spring: profiles: dev --- #测试环境 server: prot: 8082 spring: profiles: test --- #准生产环境 server: prot: 8083 spring: profiles: forecast --- #生产环境 server: prot: 8084 spring: profiles: pro
①在配置文件中指定 spring.profiles.active=dev (maven打包的时候是按照此处的配置来打的)
#properties配置文件 spring.profiles.active=pro
#yml配置文件 server: port: 8080 spring: profiles: active: pro
②idea命令行参数或虚拟机参数
③启动jar包时指定环境
java -jar ***.jar --spring.profiles.active=pro
–file:./config/
–file:./
–classpath:/config/
–classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置;
SpringBoot会从这四个位置全部加载主配置文件;互补配置;
==我们还可以通过spring.config.location来改变默认的配置文件位置==
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;
java -jar ***.jar --spring.config.location=G:/application.properties
这种方法:如果项目已经打包运行,但是需要改某个值的情况下,可以不用重新打包发布,可以在外部新加一个配置文件,然后再命令行指定文件位置来加载,从而修改配置