zoukankan      html  css  js  c++  java
  • spring boot 使用不同的profile来加载不同的配置文件

        在开发过程之中,经常需要在开发和测试环境中进行互相切换,当切换的同时需要加载相应的配置文件,因此要经常

    性的对配置文件进行相应的修改,长此以往感到十分痛苦。如果能针对开发和测试环境分别建两个不同的配置文件,当需要

    进行切换时程序能自动加载相应的配置该多好,可以使用spring提供的profile来实现这样的需求。

     1 @SpringBootApplication
     2 public class Application {
     3     
     4     public static void main(String[] args) {
     5         SpringApplication sa=new SpringApplication(Application.class);
     6         sa.setAdditionalProfiles("test");
     7         ApplicationContext appctx= sa.run(args);
     8   
     9         try {
    10             ((ConfigurableApplicationContext)appctx).close();
    11         } catch (Exception e) { /*ignore*/ }
    12     }
    13 }

    红色部分的代码是告诉spring加载带有-test后缀的配置文件。例如application-test.properties

    这样就加载了测试环境的配置。如若要切换到开发环境中只需要将profile设置为dev,

    则spring只会加载application-dev.properties,而不会加载application-test.properties

    想深入了解profile,可以详细阅读org.springframework.boot.context.config.ConfigFileApplicationListener类的源码

  • 相关阅读:
    动态规划3-最长公共子序列问题
    动态规划2-最大子段和
    动态规划1- 矩阵取数
    javac编译提示错误需要为 class、interface 或 enum
    [core python programming]chapter 7 programming MS office
    ubuntu apache nginx 启动 关闭
    ubuntu 安装 hustoj
    TCP报文段的首部格式
    守护进程
    会话session
  • 原文地址:https://www.cnblogs.com/yql1986/p/6877418.html
Copyright © 2011-2022 走看看