SpringBoot系列之profiles专题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33861603/article/details/85893986

正文:

springboot在不同环境下读取不同配置文件的一种方式,一般环境就是本地开发dev,测试test,以及生产环境prod。

简述:springboot默认读取application.yml或者application.properties。为了区别不同环境所以提供了profiles机制。若此时是在开发环境,只要有个配置文件如application_dev.yml,则在application.yml中通过spring.profiles.active=dev,当容器启动时读取配置则优先读取dev中的值(相同属性的)。

常见实现方式:

一.(springboot支持的profile)直接在application.yml中设置属性spring.profiles.active=xxx;

二.  (maven支持的profile)在maven中配置profiles节点

1.首先在pom中配置如下

<profiles>
        <!--默认开启dev-->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>这个很重要,开启
            </activation>
            <properties>
                <profileActive>dev</profileActive>
                <!-- <profiles.active>dev</profiles.active> -->
            </properties>
        </profile>
</profiles>

          2.然后在application.yml加入spring.profiles.active=@profileActive@ ,红字部分可以自己定义<properties>中的属性名。

注意点:@.....@是啥?

这是资源过滤占位符,通俗点讲跟${xxx}是一样的,但是Maven默认的是这个${},而SpringBoot扩展了properties功能,也可通过

${xxx}引用已定义的配置项,所以两者冲突。在父pom(spring-boot-starter-parent)就做了配置,将springboot的换为@xxx@并且禁用默认的maven资源过滤占位符。所以@xxx@可以引用maven中定义的属性

猜你喜欢

转载自blog.csdn.net/qq_33861603/article/details/85893986