【Spring Boot】(6)、Profile

Profile是Spring对不同环境提供不同配置功能的支持,可通过激活、指定参数等方式快速切换环境。

1、多Profile文件(Properties格式)

在主配置文件编写的时候,文件名可以是application-{profile}.properties

  • application.properties: 默认全局配置文件

  • application-dev.properties: 开发环境

  • application-prod.properties: 生产环境

  • application-test.properties: 测试环境


2、yml支持多文档块方式

server:
  port: 8080
spring:
  profiles:
    active: dev

---
server:
  port: 8084
spring:
  profiles: dev


---
server:
  port: 8085
spring:
  profiles: prod


---
server:
  port: 8086
spring:
  profiles: test


3、激活指定Profile

  • 在全局配置文件中指定spring.profiles.active=...来指定当前的环境,并使用对应的application-{profile}.properties或者application.yml所对应profile下的配置作为当前环境使用的配置信息。

  • 虚拟机参数:Edit Configurations ---> VM options ---->-Dspring.profiles.active=...

  • 命令行:

    • 打包用命令java -jar xxxx.jar --spring.profiles.active=...

    • IDEA运行设置:Edit Configurations ---> Program arguments ----> --spring.profiles.active=xx



====================打个广告,欢迎关注====================

QQ:
412425870
微信公众号:Cay课堂

csdn博客:
http://blog.youkuaiyun.com/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)




点击群号或者扫描二维码即可加入QQ群:

180479701(2群)




### Spring Boot Profile Usage and Configuration In the context of Spring Boot, profiles serve as a mechanism to segregate configurations based on environments such as development, testing, or production. The application can load different settings depending on which profile is active. Configuration properties are managed through `application.properties` or `application.yml`, where specific files like `application-{profile}.properties` allow for environment-specific overrides[^1]. For instance, an `application-dev.properties` file would contain settings applicable only when the "dev" profile is activated. The activation of these profiles occurs via several methods: - **Command Line Argument:** By passing `-Dspring.profiles.active=profileName`. - **Environment Variable:** Setting `SPRING_PROFILES_ACTIVE=profileName`. - **Programmatic Approach:** Before invoking `SpringApplication.run()`, one may call `SpringApplication.setAdditionalProfiles("profileName")`. Implementing the `ConfigurableEnvironment` interface also provides flexibility in setting up profiles programmatically[^3]. For applications utilizing Spring Cloud Vault, secrets management integrates seamlessly with profiles by searching paths that include both the application name and its active profile(s). This ensures secure storage and retrieval of sensitive information without hardcoding it into source-controlled repositories[^4]. When developing multi-environment applications using Spring Boot, leveraging profiles simplifies managing varying configurations across distinct deployment scenarios while maintaining codebase consistency. ```java // Example demonstrating programmatic profile setup before running SpringApplication. import org.springframework.boot.SpringApplication; public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setAdditionalProfiles("prod"); // Set 'prod' as additional profile app.run(args); } } ``` --related questions-- 1. How does Spring Boot handle multiple active profiles simultaneously? 2. What mechanisms exist within Spring Boot for overriding default property values per environment? 3. Can you provide examples of how security credentials might be stored securely using Spring Cloud Vault alongside profiles? 4. In what ways do externalized configuration sources interact with each other in terms of precedence order during startup?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值