spring boot实现profiles动态切换

Spring Boot支持在不同的环境下使用不同的配置文件,该技术非常有利于持续集成,在构建项目的时候只需要使用不同的构建命令就可以生成不同运行环境下war包,而不需要手动切换配置文件。

具体做法:

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
                  
                     **/*.xml
                     **/*.yml
                  
               
               
                  src/main/resources/
                  false
                  
                     **/*.xml
                     **/*.yml
                  
               
            
         
      
   

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

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

import org.springframework.boot.SpringApplication;
import org.springframework.core.env.Environment;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * Utility class to load a Spring profile to be used as default
 * when there is no spring.profiles.active set in the environment or as command line argument.
 * If the value is not available in application.yml then dev profile will be used as default.
 */
public final class DefaultProfileUtil {
 
    private static final String SPRING_PROFILE_DEFAULT = "spring.profiles.default";
 
    private DefaultProfileUtil(){
    }
 
    /**
     * Set a default to use when no profile is configured.
     *
     * @param app the spring application
     */
    public static void addDefaultProfile(SpringApplication app) {
        Map defProperties =  new HashMap<>();
        /*
        * The default profile to use when no other profiles are defined
        * This cannot be set in the application.yml file.
        * See https://github.com/spring-projects/spring-boot/issues/1219
        */
        defProperties.put(SPRING_PROFILE_DEFAULT, Constants.SPRING_PROFILE_DEVELOPMENT);
        app.setDefaultProperties(defProperties);
        System.out.println(app);
    }
 
    /**
     * Get the profiles that are applied else get default profiles.
     */
    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----------------------------------------------------------\n\t" +
                  "Application '{}' is running! Access URLs:\n\t" +
                  "Local: \t\thttp://localhost:{}\n\t" +
                  "----------------------------------------------------------",
            env.getProperty("spring.application.name"),
            env.getProperty("server.port"));
   }
}

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

spring boot实现profiles动态切换_第1张图片
blob-2.png

6、构建项目:

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

spring boot实现profiles动态切换_第2张图片
blob.png

你可能感兴趣的:(spring boot实现profiles动态切换)