SpringBoot基础系列-使用Profiles

原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996884.html

SpringBoot基础系列-使用Profile

概述

Profile主要用于区分不同的环境。

使用方法

@Profile

在某个类、或者方法上添加@Profile注解,指定具体的profile环境标签,那么只又在该profile处于active的情况下该类,方法才会被加载、执行。

@Profile({"dev","test"})
public class Xxx{
    
    @Profile({"dev"})
    @Bean
    public Xxx xxx(){
        return new Xxx();
    }
}

多环境配置

properties配置文件

使用properties配置文件实现多环境配置,只能通过添加多个application-{profile}.properties来实现。
比如:application-dev.properties,application-test.properties

YAML配置文件

使用YAML实现多环境配置要简单的多,只需要一个文件即可,application.yml
在文件中使用---来区分多个环境,每个环境都需要配置spring.profile属性,不配置的属于默认环境

server:
  port: 8080
#属性映射测试
app:
  name: springdemo
  size: 100M
  user: weiyihaoge
  version: 0.0.1
---
spring:
  profiles: dev
server:
  port: 8081
---
spring:
  profiles: test
server:
  port: 8082
---
spring:
  profiles: pro
server:
  port: 8083

激活profiles

可以在命令行参数、系统参数、application.properties等处进行配置

命令行
--spring.profiles.active=dev
application.properties
spring.profiles.active=dev

添加profiles

我们可以在不修改已启动的profiles的基础上添加新的profiles
使用spring.profiles.include属性进行配置
还可以使用编程的方式实现,使用如下的方式添加:

SpringApplication.setAdditionalProfiles("development");

转载于:https://www.cnblogs.com/V1haoge/p/9996884.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值