zoukankan      html  css  js  c++  java
  • 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");
    
  • 相关阅读:
    了解委托(Delegate)
    C#中事件的一些总结
    Devexpress Xtrareport 并排报表
    Xtrareport 交叉报表
    Xtrareport 多栏报表
    Xtrareport 报表的一些属性及控件
    UI前端开发都是做什么的以及html、css、php、js等究竟是神马关系
    url,href,src之间的区别
    join()的用法
    爬取百度百科
  • 原文地址:https://www.cnblogs.com/V1haoge/p/9996884.html
Copyright © 2011-2022 走看看