栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot实现profiles动态切换的示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot实现profiles动态切换的示例

具体做法:

1、首先在pom中添加profiles:


  
   dev
   
     true
   
   
     dev
   
   
     
      org.springframework.boot
      spring-boot-devtools
      true
     
     
      org.springframework.boot
      spring-boot-starter-undertow
     
   
  
 
  
   prod
   
     
      org.springframework.boot
      spring-boot-starter-undertow
     
   
   
     prod
   
  

dev指开发模式,prod指生产模式,如需其他模式,只需要添加profile即可.

2、在pom.xml的build中添加plugin:


  org.apache.maven.plugins
  maven-resources-plugin
  ${maven-resources-plugin.version}
  
   
     default-resources
     validate
     
      copy-resources
     
     
      target/classes
      false
      
 #
      
      
 
  src/main/resources/
  true
  
    ***.yml
  
 
 
  src/main/resources/
  false
  
    ***.yml
  
 
      
     
   
  

该配置用来在打包的时候修改配置文件。

3、编写DefaultProfileUtil工具类来添加默认启动配置文件:

import org.springframework.boot.SpringApplication;
import org.springframework.core.env.Environment;
 
import java.util.HashMap;
import java.util.Map;
 

public final class DefaultProfileUtil {
 
  private static final String SPRING_PROFILE_DEFAULT = "spring.profiles.default";
 
  private DefaultProfileUtil(){
  }
 
  
  public static void addDefaultProfile(SpringApplication app) {
    Map defProperties = new HashMap<>();
    
    defProperties.put(SPRING_PROFILE_DEFAULT, Constants.SPRING_PROFILE_DEVELOPMENT);
    app.setDefaultProperties(defProperties);
    System.out.println(app);
  }
 
  
  public static String[] getActiveProfiles(Environment env) {
    String[] profiles = env.getActiveProfiles();
    if (profiles.length == 0) {
      return env.getDefaultProfiles();
    }
    return profiles;
  }
}
public class Constants {
 
  public static final String SPRING_PROFILE_DEVELOPMENT = "dev";
  public static final String SPRING_PROFILE_PRODUCTION = "prod";
  private Constants() {
  }
}

4、修改application.yml配置文件,添加(采用application.properties文件):

spring:
  profiles:
   active: #spring.profiles.active#

maven的构建的时候会替换#spring.profiles.active#

5、修改项目的启动类:

@SpringBootApplication
public class Demo1Application {
 
  private static final Logger log = LoggerFactory.getLogger(Demo1Application.class);
 
  public static void main(String[] args) {
   SpringApplication app = new SpringApplication(Demo1Application.class);
   DefaultProfileUtil.addDefaultProfile(app);
   Environment env = app.run(args).getEnvironment();
   log.info("n----------------------------------------------------------nt" +
  "Application '{}' is running! Access URLs:nt" +
  "Local: tthttp://localhost:{}nt" +
  "----------------------------------------------------------",
      env.getProperty("spring.application.name"),
      env.getProperty("server.port"));
  }
}

以上修改完成之后,在启动的时候会显示:The following profiles are active: dev 默认dev模式切换成功。

6、构建项目:

采用mvn clean package -Pprod命令构建,最后的配置文件会被改成:

以上就是spring boot实现profiles动态切换的示例的详细内容,更多关于spring boot实现profiles动态切换的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号