【Springboot总结】6. Profile -- 配置

总结

1. 配置文件的方式激活不同的生产环境

2. 命令行的方式激活不同的生产环境

 

一、Application.properties

 

之所以使用多Profile,是为了应付不同的生产环境。

 

1、多Profile文件

      application-dev.properties

      application-prod.properties

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml,默认使用application.properties的配置

 

2. 激活指定的profile

默认是使用application.properties文件里面的配置,我们可以在application.properties里面配置

spring.profiles.active=dev

这样来激活指定的环境!

 

二、application.yml

 

1.yml支持多文档块方式

server:
  port: 8081
spring:
  profiles:
    active: prod

---
server:
  port: 8083
spring:
  profiles: dev 


---

server:
  port: 8084
spring:
  profiles: prod  

使用---来区分文档。我们可以看到上面的配置是存在3个文档对象的!

 

2. 激活指定的环境

 

三、激活指定的环境

除了上面在配置文件中配置,还可以使用下面的方式激活环境

1.  命令行

   

上述配置好之后,会覆盖配置文件里面的配置!点击运行项目就可以了!

 

或者打包项目

java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;

这样运行,也是OK的!

 

2. 虚拟机参数

 

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.9) 2025-03-29 23:30:44.927 INFO 10080 --- [ main] com.example.SpringbootApplication : Starting SpringbootApplication using Java 1.8.0_341 on DESKTOP-EUNUR1G with PID 10080 (D:\毕业设计\manager\springboot\target\classes started by DELL in D:\毕业设计\manager) 2025-03-29 23:30:44.929 INFO 10080 --- [ main] com.example.SpringbootApplication : No active profile set, falling back to default profiles: default 2025-03-29 23:30:45.709 INFO 10080 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9090 (http) 2025-03-29 23:30:45.716 INFO 10080 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-03-29 23:30:45.716 INFO 10080 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56] 2025-03-29 23:30:45.800 INFO 10080 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-03-29 23:30:45.800 INFO 10080 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 836 ms Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter. Parsed mapper file: 'file [D:\毕业设计\manager\springboot\target\classes\mapper\AdminMapper.xml]' Parsed mapper file: 'file [D:\毕业设计\manager\springboot\target\classes\mapper\NoticeMapper.xml]' ,------. ,--. ,--. ,--. | .--. ' ,--,--. ,---. ,---. | '--' | ,---. | | ,---. ,---. ,--.--. | '--' | ' ,-. | | .-. | | .-. : | .--. | | .-. : | | | .-. | | .-. : | .--' | | -
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值