Home > Java > javaTutorial > body text

How to implement SpringBoot profile multi-environment switching

WBOY
Release: 2023-05-17 10:04:11
forward
1180 people have browsed it

SpringBoot's profile multi-environment switching

1. Configure profile and build in the pom file
<!-- 环境 -->
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <!--默认激活配置-->
                <activebydefault>true</activebydefault>
            </activation>
            <properties>
                <!--当前环境-->
                <profile.name>dev</profile.name>
                <!--Nacos配置中心地址-->
                <config.server-addr>127.0.0.1:8848</config.server-addr>
                <!--Nacos配置中心命名空间,用于支持多环境.这里必须使用ID,不能使用名称,默认为空-->
                <config.namespace>DEV</config.namespace>
                <!--Nacos服务发现地址-->
                <discovery.server-addr>127.0.0.1:8848</discovery.server-addr>
                <discovery.namespace>DEV</discovery.namespace>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <!--当前环境-->
                <profile.name>test</profile.name>
                <!--Nacos配置中心地址-->
                <config.server-addr>127.0.0.1:8848</config.server-addr>
                <!--Nacos配置中心命名空间,用于支持多环境.这里必须使用ID,不能使用名称,默认为空-->
                <config.namespace>TEST</config.namespace>
                <!--Nacos服务发现地址-->
                <discovery.server-addr>127.0.0.1:8848</discovery.server-addr>
                <discovery.namespace>TEST</discovery.namespace>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <!--当前环境-->
                <profile.name>prod</profile.name>
                <!--Nacos配置中心地址-->
                <config.server-addr>127.0.0.1:8848</config.server-addr>
                <!--Nacos配置中心命名空间,用于支持多环境.这里必须使用ID,不能使用名称,默认为空-->
                <config.namespace>PROD</config.namespace>
                <!--Nacos服务发现地址-->
                <discovery.server-addr>127.0.0.1:8848</discovery.server-addr>
                <discovery.namespace>PROD</discovery.namespace>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 处理文件时替换文件中的变量 -->
                <filtering>true</filtering>
                <excludes>
                    <!-- 打包时排除文件,可自行添加test.yml -->
                    <exclude>application.yml</exclude>
                    <exclude>application-dev.yml</exclude>
                    <exclude>application-test.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <!-- 打包时所包含得文件 -->
                <includes>
                    <include>application.yml</include>
                    <include>application-${profiles}.yml</include>
                </includes>
            </resource>
        </resources>
    </build>
Copy after login

2. Create three files dev\test
How to implement SpringBoot profile multi-environment switching

3. Bootstrap source file content

spring:
  profiles:
    active: test
Copy after login

4. bootstrap-dev content

server:
  port: 8080
spring:
  profiles: dev
Copy after login

5. bootstrap-test content

server:
  port: 8080
spring:
  profiles: test
Copy after login

6. Switch
How to implement SpringBoot profile multi-environment switching

The above is the detailed content of How to implement SpringBoot profile multi-environment switching. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template