springboot 多profile配置

来源:互联网 发布:js弹出图片窗口 编辑:程序博客网 时间:2024/06/06 14:12

一般来说,我们所有的配置都在application.properties里面,

这个时候我们可以分多个配置文件适配不同的环境。

开发环境

连接本地数据库
用户名
密码
日志存放在F盘的xxx目录

生产环境

服务器数据库
用户名
密码
日志存放在 /root/xxx之类的linux目录上


好,开始
在根目录(resources)下,有三个属性文件,除了application.properties,还有一个开发环境属性和生产环境属性的文件,需要遵循application-{profile}.properties这种命名格式,这样就能提供特定于profile的属性了。

这里写图片描述

在application.properties里先指定用哪一种环境

#生产环境spring.profiles.active=prod

这样,程序就会选用prod文件里配置的数据库和日志目录等相应的属性

所有环境可以共用的属性就放在application.properties 就好

不同环境下属性值不同的在各自对应的环境中配置

例如:
一般开发环境下日志的级别是dubug

开发环境

server.port=8866spring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql = truespring.datasource.url=jdbc:mysql://localhost:3306/scholarship?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=rootlogging.path=F:\\ANDLINKS-PROJECT\\Andlinks-Yourturn\\logslogging.file=devlog.loglogging.level.root=debug

 
生产环境下日志级别是warn或者更高级

生产环境:

server.port=8855spring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql = truespring.datasource.url=jdbc:mysql://192.168.1.22:3306/scholarship?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=andlinkslogging.path=/root/ch/black/logslogging.file=pro.loglogging.level.root=warn