springBoot配置spring.profiles.active多环境

springBoot动态配置多环境(dev/test/prod)数据信息读取

pom文件配置

maven添加对应项目构建索要读取的目录与环境动态配置参数 application-${spring.profiles.active}.yaml
默认读取dev开发环境下的配置信息,可以通过IDE进行不同的配置文件信息读取
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                <include>**/*.xml</include>
                <include>application.yaml</include>
                <include>application-${spring.profiles.active}.yaml</include>
                </includes>
            </resource>
        </resources>
    </build>
    
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>docker</id>
            <properties>
                <spring.profiles.active>docker</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

YAML文件配置

根据yaml文件的加载项配置,初始化项目优先读取application.yaml下的住配置文件下面会分别贴出对应配置文件
的配置信息, 此处配置完成后,按住 Ctrl+鼠标左键可以进入到对应的pom文件中,配置正确,还需要额外注意一定要
加上双引号讲application.yaml主配置文件中的动态信息包裹,否则会出现
加载错误 : '@' that cannot start any token. (Do not use @ for indentation)
#application.yaml 主配置文件信息
spring:
  profiles:
    #注意此处需要用双引号/单引号将配置的动态读取环境变量,以确保项目可以正常加载
    active: "@spring.profiles.active@"
#application-dev.yaml 开发环境配置文件信息
## MySQL configuration
db:
  type: mysql
  url: XXXXX
  username: root
  password: root
配置完成之后要怎么验证呢,项目初始化的时候查看日志相关配置,会读取出对应的环境信息
出现  The following profiles are active: dev 此处配置不同环境下加载不同的数据源与配置信息
就完成了.
2020-07-16 11:02:01.008  INFO 15872 --- [           main] c.lenovo.hawk.HawkDashboardApplication   : The following profiles are active: dev
2020-07-16 11:02:05.702  INFO 15872 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值