SpringBoot——Profile多環境支持

1.多profile文件形式

主配置文件編寫時, 文件名能夠是application-{profile}.properties/ymlapp

默認使用的application.properties的配置.jvm

 

2.多profile文檔塊模式

yml支持多文檔塊模式ide

  能夠經過啓動時端口號判斷啓用的環境.this

3.激活方式

命令行

 

配置文件中指定

 

 jvm參數

 

 

另:經過程序獲取profile的值spa

https://blog.csdn.net/Fmuma/article/details/82787500.net

使用profile幫我解決了不一樣環境使用不一樣配置的問題,那麼若是咱們須要在代碼中獲取這個profile的值怎麼處理呢?命令行

@Component
public class ProfileUtil implements ApplicationContextAware {

    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.context = applicationContext;
    }

    // 獲取當前環境參數  exp: dev,prod,test
    public static String getActiveProfile() {
        String []profiles = context.getEnvironment().getActiveProfiles();
        if( ! ArrayUtils.isEmpty(profiles)){
            return profiles[0];
        }
        return "";
    }
}
相關文章
相關標籤/搜索