如何在Spring Boot中配置spring.profiles属性

本篇文章为大家展示了如何在Spring Boot中配置spring.profiles属性,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

Spring Boot配置特定属性spring.profiles

SpringBoot能使用application- {你的自定义profile名称myProfileName} .properties模式添加任何你指定配置文件到其属性文件。

要加载特定的配置文件属性文件,我们可以使用命令行选项-Dspring.profiles.active = myProfileName。

缺省默认SpringBoot是加载application.properties,无需任何-Dspring.profile.active选项,或使用-Dspring.profiles.active = default来加载。默认属性文件也可以命名为application-default.properties

默认配置文件application.properties中指定的任何属性将被你指定加载的配置文件中的的属性覆盖。

也可以在application.properties中指定激活配置文件。

spring.profiles.active=prod

比如你有三个配置文件:

src/main/resources/application.properties(默认的)

src/main/resources/application-dev.properties(你指定的dev)

src/main/resources/application-prod.properties(你指定的prod)

如果在application.properties中有:

spring.profiles.active=prod

那么SpringBoot将加载application-prod.properties内容。

如果你在代码中使用配置文件中的变量:

@Component
<b>public</b> <b>class</b> ClientBean {
 @Value(<font>"${app.window.width}"</font><font>)
 <b>private</b> <b>int</b> width;
 @Value(</font><font>"${app.window.height}"</font><font>)
 <b>private</b> <b>int</b> height;
</font>

如果application-prod.properties和application.properties都有app.window.width和app.window.height,那么以prod中配置的值为主。

spring.profile.include属性

在application-prod.properties还可以加入

spring.profiles.include=throttling,db

这是无条件地添加活动配置文件(以逗号分隔)。此属性添加的配置文件不会根据某些条件或命令行开关决定是否添加,而是始终无条件添加它们。

上述配置是就加载了:

src/main/resources/application-throttling.properties
src/main/resources/application-db.properties

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

上述内容就是如何在Spring Boot中配置spring.profiles属性,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。