好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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中有:

?

1

spring.profiles.active=prod

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

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

?

1

2

3

4

5

6

7

@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还可以加入

?

1

spring.profiles.include=throttling,db

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

上述配置是就加载了:

?

1

2

src/main/resources/application-throttling.properties

src/main/resources/application-db.properties

这两个配置文件中的内容。

总结

以上所述是小编给大家介绍的spring boot配置特定属性spring.profiles,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://HdhCmsTestjdon测试数据/50600

查看更多关于Spring Boot配置特定属性spring.profiles的方法的详细内容...

  阅读:10次