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

1、首先在pom中添加profiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<profiles>
  <profile>
   <id>dev</id>
   <activation>
     <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
     <spring.profiles.active>dev</spring.profiles.active>
   </properties>
   <dependencies>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
     </dependency>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
     </dependency>
   </dependencies>
  </profile>
  
  <profile>
   <id>prod</id>
   <dependencies>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
     </dependency>
   </dependencies>
   <properties>
     <spring.profiles.active>prod</spring.profiles.active>
   </properties>
  </profile>
</profiles>
dev指开发模式,prod指生产模式,如需其他模式,只需要添加profile即可.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>${maven-resources-plugin.version}</version>
  <executions>
   <execution>
     <id>default-resources</id>
     <phase>validate</phase>
     <goals>
      <goal>copy-resources</goal>
     </goals>
     <configuration>
      <outputDirectory>target/classes</outputDirectory>
      <useDefaultDelimiters>false</useDefaultDelimiters>
      <delimiters>
        <delimiter>#</delimiter>
      </delimiters>
      <resources>
        <resource>
         <directory>src/main/resources/</directory>
         <filtering>true</filtering>
         <includes>
           <include>**/*.xml</include>
           <include>**/*.yml</include>
         </includes>
        </resource>
        <resource>
         <directory>src/main/resources/</directory>
         <filtering>false</filtering>
         <excludes>
           <exclude>**/*.xml</exclude>
           <exclude>**/*.yml</exclude>
         </excludes>
        </resource>
      </resources>
     </configuration>
   </execution>
  </executions>
</plugin>
该配置用来在打包的时候修改配置文件。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 <code>spring.profiles.active</code> set in the environment or as command line argument.
 * If the value is not available in <code>application.yml</code> then <code>dev</code> 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<String, Object> defProperties = new HashMap<>();
    /*
    * The default profile to use when no other profiles are defined
    * This cannot be set in the <code>application.yml</code> 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;
  }
}
1
2
3
4
5
6
7
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文件):

1
2
3
spring:
  profiles:
   active: #spring.profiles.active#
maven的构建的时候会替换#spring.profiles.active#

5、修改项目的启动类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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模式切换成功。

6、构建项目:

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

以上就是spring boot实现profiles动态切换的示例的详细内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值